{"id":10503,"date":"2025-08-20T11:21:22","date_gmt":"2025-08-20T11:21:22","guid":{"rendered":"https:\/\/www.kaashivinfotech.com\/blog\/?p=10503"},"modified":"2025-08-20T11:21:22","modified_gmt":"2025-08-20T11:21:22","slug":"usestate-in-react-js-guide-examples","status":"publish","type":"post","link":"https:\/\/www.kaashivinfotech.com\/blog\/usestate-in-react-js-guide-examples\/","title":{"rendered":"useState in React JS: A Complete Beginner\u2019s Guide (with Examples)"},"content":{"rendered":"<p>If you are new to learning React JS, the very first hook that you will encounter will the useState hook. Understanding useState in React JS is extremely important, because it is the fundamental way of managing dynamic data inside your components.<\/p>\n<p>In this article, we will break down useState in React, go over why it is important, provide examples for each data type (string, number, array, object), and answer common questions faced by beginners.<\/p>\n<p>You will walk away knowing not only how the useState hook works in React JS, but also using the useState hook proudly in real-world applications.<\/p>\n<h2>What is useState in React JS?<\/h2>\n<figure id=\"attachment_10507\" aria-describedby=\"caption-attachment-10507\" style=\"width: 1400px\" class=\"wp-caption aligncenter\"><img fetchpriority=\"high\" decoding=\"async\" class=\"wp-image-10507 size-full\" src=\"https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/08\/useState-in-React-JS-1.webp\" alt=\"usestate in react js\" width=\"1400\" height=\"709\" srcset=\"https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/08\/useState-in-React-JS-1.webp 1400w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/08\/useState-in-React-JS-1-300x152.webp 300w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/08\/useState-in-React-JS-1-1024x519.webp 1024w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/08\/useState-in-React-JS-1-768x389.webp 768w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/08\/useState-in-React-JS-1-380x192.webp 380w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/08\/useState-in-React-JS-1-800x405.webp 800w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/08\/useState-in-React-JS-1-1160x587.webp 1160w\" sizes=\"(max-width: 1400px) 100vw, 1400px\" \/><figcaption id=\"caption-attachment-10507\" class=\"wp-caption-text\">useState in React JS<\/figcaption><\/figure>\n<p data-start=\"2081\" data-end=\"2323\">The useState Hook gives your functional components easy-to-use built-in state functionality. Before Hooks in React JS, you could only store a state in Class components, so functional components were limited to stateless UI rendering. Now that the useState Hook is part of React, it can create an interface for dynamic storage and data updates in your functional components.<\/p>\n<p>\ud83d\udc49 In simple terms: useState allows your React app to <strong data-start=\"2379\" data-end=\"2414\">remember values between renders<\/strong>.<\/p>\n<p data-start=\"2419\" data-end=\"2429\">Example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">import React, { useState } from \"react\";\u00a0\r\n\r\nfunction Counter()\r\n\r\n{\u00a0\r\n\r\nconst [count, setCount] = useState(0);\u00a0\u00a0\r\n\r\nreturn (\u00a0\u00a0\u00a0\r\n\r\n&lt;div&gt;\u00a0\u00a0\u00a0\u00a0\u00a0\r\n\r\n&lt;h2&gt;You clicked {count} times&lt;\/h2&gt;\u00a0\u00a0\u00a0\u00a0\u00a0\r\n\r\n&lt;button onClick={() =&gt; setCount(count + 1)}&gt;Click Me&lt;\/button&gt;\u00a0\u00a0\u00a0\r\n\r\n&lt;\/div&gt;\u00a0\r\n\r\n);\r\n\r\n}<\/pre>\n<p data-start=\"2695\" data-end=\"2793\">Here, count is the <strong data-start=\"2716\" data-end=\"2734\">state variable<\/strong>, and setCount is the <strong data-start=\"2758\" data-end=\"2790\">function to update the state<\/strong>.<\/p>\n<h2>Syntax of useState in React<\/h2>\n<p data-start=\"2835\" data-end=\"2886\">The syntax of <strong data-start=\"2849\" data-end=\"2873\">useState in React JS<\/strong> is simple:<\/p>\n<p><span data-state=\"closed\">const<\/span> [state, setState] = useState(initialValue);<\/p>\n<ul>\n<li data-start=\"2952\" data-end=\"2994\">state \u2192 current value of your variable<\/li>\n<li data-start=\"2997\" data-end=\"3040\">setState \u2192 function to update the value<\/li>\n<li data-start=\"3043\" data-end=\"3100\">initialValue \u2192 default value when the component loads<\/li>\n<\/ul>\n<h2>Why Do We Need useState in React?<\/h2>\n<p>The best way to understand when useState is useful is to imagine building an app in which:<\/p>\n<ul>\n<li>A button must track the number of clicks<\/li>\n<li>A form temporarily stores user input<\/li>\n<li>A modal can toggle its state between open and closed<\/li>\n<\/ul>\n<p>Without the useState Hook, you would have no access to make your components &#8220;remember&#8221; something. Each render would reset the state to the default value in your component.<\/p>\n<h2>Different Examples of useState in React JS<\/h2>\n<h3 data-start=\"3513\" data-end=\"3544\">1. <strong data-start=\"3520\" data-end=\"3544\">useState with String<\/strong><\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">function Greeting()\r\n\r\n{\u00a0\r\n\r\nconst [name, setName] = useState(\"John\");\u00a0\u00a0\r\n\r\nreturn (\u00a0\u00a0\u00a0\r\n\r\n&lt;div&gt;\u00a0\u00a0\u00a0\u00a0\u00a0\r\n\r\n&lt;p&gt;Hello, {name}!&lt;\/p&gt;\u00a0\u00a0\u00a0\u00a0\u00a0\r\n\r\n&lt;button onClick={() =&gt; setName(\"Sarah\")}&gt;Change Name&lt;\/button&gt;\u00a0\u00a0\u00a0\r\n\r\n&lt;\/div&gt;\u00a0\r\n\r\n);\r\n\r\n}<\/pre>\n<h3 data-start=\"3759\" data-end=\"3791\">2. <strong data-start=\"3766\" data-end=\"3791\">useState with Numbers<\/strong><\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">const [count, setCount] = useState(0);<\/pre>\n<h3 data-start=\"3843\" data-end=\"3875\">3. <strong data-start=\"3850\" data-end=\"3875\">useState with Boolean<\/strong><\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">const [isVisible, setIsVisible] = useState(false);<\/pre>\n<h3 data-start=\"3939\" data-end=\"3971\">4. <strong data-start=\"3946\" data-end=\"3971\">useState with Objects<\/strong><\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">const [user, setUser] = useState({ name: \"Alex\", age: 25 });<\/pre>\n<h3 data-start=\"4045\" data-end=\"4076\">5. <strong data-start=\"4052\" data-end=\"4076\">useState with Arrays<\/strong><\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">const [items, setItems] = useState([\"Apple\", \"Banana\"]);<\/pre>\n<p data-start=\"4146\" data-end=\"4292\">\ud83d\udc49 Tip: When updating objects or arrays with <strong data-start=\"4191\" data-end=\"4215\">useState in React JS<\/strong>, always use the <strong data-start=\"4232\" data-end=\"4251\">spread operator<\/strong> to avoid overwriting the entire state.<\/p>\n<h2>Updating State Correctly in React<\/h2>\n<p data-start=\"4340\" data-end=\"4427\">One common mistake with <strong data-start=\"4364\" data-end=\"4385\">useState in React<\/strong> is trying to update the state directly.<\/p>\n<p data-start=\"4429\" data-end=\"4439\">\u274c Wrong:<\/p>\n<p><span data-state=\"closed\">count = count + <\/span>1;<\/p>\n<p data-start=\"4471\" data-end=\"4483\">\u2705 Correct:<\/p>\n<p><span data-state=\"closed\">setCount<\/span>(count + 1);<\/p>\n<h2>Difference Between State and Props in React<\/h2>\n<ul>\n<li data-start=\"4575\" data-end=\"4634\"><strong data-start=\"4575\" data-end=\"4584\">State<\/strong> \u2192 Managed inside a component (using useState)<\/li>\n<li data-start=\"4637\" data-end=\"4692\"><strong data-start=\"4637\" data-end=\"4646\">Props<\/strong> \u2192 Passed <strong data-start=\"4656\" data-end=\"4680\">from parent to child<\/strong> component<\/li>\n<\/ul>\n<p data-start=\"4694\" data-end=\"4769\">Understanding this difference helps you structure your React apps better.<\/p>\n<h2>Common Mistakes with useState in React JS<\/h2>\n<p>Some common mistakes with very new React developers when using useState:<\/p>\n<ul>\n<li>Updating state directly instead of using the setter<\/li>\n<li>Forgetting that state updates are asynchronous<\/li>\n<li>Not initializing state<\/li>\n<li>Overusing multiple state and not combining them into an object<\/li>\n<\/ul>\n<h2>FAQs on useState in React<\/h2>\n<h3 data-start=\"5086\" data-end=\"5134\">Q1: What is useState in React JS used for?<\/h3>\n<p data-start=\"5135\" data-end=\"5209\">It\u2019s used for adding state (data that changes) in functional components.<\/p>\n<h3 data-start=\"5211\" data-end=\"5272\">Q2: Can I use multiple useState hooks in one component?<\/h3>\n<p data-start=\"5273\" data-end=\"5312\">Yes! You can use as many as you like.<\/p>\n<h3 data-start=\"5314\" data-end=\"5378\">Q3: What is the difference between useState and useEffect?<\/h3>\n<ul>\n<li data-start=\"5381\" data-end=\"5411\"><strong data-start=\"5381\" data-end=\"5393\">useState<\/strong> \u2192 stores values<\/li>\n<li data-start=\"5414\" data-end=\"5485\"><strong data-start=\"5414\" data-end=\"5427\">useEffect<\/strong> \u2192 runs side effects (e.g., fetching data, updating DOM)<\/li>\n<\/ul>\n<h2>Real-World Example of useState in React<\/h2>\n<figure id=\"attachment_10508\" aria-describedby=\"caption-attachment-10508\" style=\"width: 666px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" class=\" wp-image-10508\" src=\"https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/08\/Real-World-Example-of-useState-in-React.webp\" alt=\"usestate in react js\" width=\"666\" height=\"444\" srcset=\"https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/08\/Real-World-Example-of-useState-in-React.webp 1536w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/08\/Real-World-Example-of-useState-in-React-300x200.webp 300w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/08\/Real-World-Example-of-useState-in-React-1024x683.webp 1024w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/08\/Real-World-Example-of-useState-in-React-768x512.webp 768w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/08\/Real-World-Example-of-useState-in-React-380x253.webp 380w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/08\/Real-World-Example-of-useState-in-React-800x533.webp 800w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/08\/Real-World-Example-of-useState-in-React-1160x773.webp 1160w\" sizes=\"(max-width: 666px) 100vw, 666px\" \/><figcaption id=\"caption-attachment-10508\" class=\"wp-caption-text\">Real-World Example of useState in React<\/figcaption><\/figure>\n<p data-start=\"5542\" data-end=\"5588\">Let\u2019s build a <strong data-start=\"5556\" data-end=\"5570\">to-do list<\/strong> using useState:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">import React, { useState } from \"react\";\u00a0\r\n\r\nfunction TodoApp()\r\n\r\n{\u00a0\r\n\r\nconst [tasks, setTasks] = useState([]);\u00a0\r\n\r\nconst [input, setInput] = useState(\"\");\u00a0\u00a0\r\n\r\nconst addTask = () =&gt; {\u00a0\u00a0\u00a0\r\n\r\nsetTasks([...tasks, input]);\u00a0\u00a0\u00a0\r\n\r\nsetInput(\"\");\u00a0\r\n\r\n};\u00a0\u00a0\r\n\r\nreturn (\u00a0\u00a0\u00a0\r\n\r\n&lt;div&gt;\u00a0\u00a0\u00a0\u00a0\u00a0\r\n\r\n&lt;h2&gt;My To-Do List&lt;\/h2&gt;\u00a0\u00a0\u00a0\u00a0\u00a0\r\n\r\n&lt;input value={input} onChange={(e) =&gt; setInput(e.target.value)} \/&gt;\u00a0\u00a0\u00a0\u00a0\u00a0\r\n\r\n&lt;button onClick={addTask}&gt;Add Task&lt;\/button&gt;\u00a0\u00a0\u00a0\u00a0\u00a0\r\n\r\n&lt;ul&gt;\r\n\r\n\u00a0 {tasks.map((task, index) =&gt; (\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\r\n\r\n&lt;li key={index}&gt;{task}&lt;\/li&gt;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\r\n\r\n\u00a0 ))}\u00a0\u00a0\u00a0\u00a0\u00a0\r\n\r\n&lt;\/ul&gt;\u00a0\u00a0\u00a0\r\n\r\n&lt;\/div&gt;\u00a0\r\n\r\n);\r\n\r\n}<\/pre>\n<p data-start=\"6135\" data-end=\"6201\">Here we used <strong data-start=\"6148\" data-end=\"6169\">useState in React<\/strong> with both strings and arrays.<\/p>\n<h2>Final Thoughts<\/h2>\n<p data-start=\"6230\" data-end=\"6445\">Learning to use the <strong>useState in React JS<\/strong> is the first step in developing toward an understanding of React Hooks overall. Once you become good with using useState you will find it much easier to learn and understand useEffect, useContext, and useReducer.<\/p>\n<p>If you are a beginning React developer I would suggest trying to build small apps with just the useState Hook in React. Some app examples would be a counter, data entry form, or a simple todo list.<\/p>\n<h2>Related Reads<\/h2>\n<ul>\n<li><a href=\"https:\/\/www.kaashivinfotech.com\/blog\/react-js-icons-guide-and-tips\/\">React JS Icons: The Complete Guide to Using Icons in React<\/a><\/li>\n<li><a href=\"https:\/\/www.kaashivinfotech.com\/blog\/react-js-vs-react-native-differences\/\">React js vs React Native: Key Differences Explained<\/a><\/li>\n<li><a href=\"https:\/\/www.wikitechy.com\/mastering-function-keys-the-developers-ultimate-cheat-sheet\/\" target=\"_blank\" rel=\"noopener\">Mastering Function Keys: The Developer\u2019s Ultimate Cheat Sheet<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>If you are new to learning React JS, the very first hook that you will encounter will the useState hook. Understanding useState in React JS is extremely important, because it is the fundamental way of managing dynamic data inside your components. In this article, we will break down useState in React, go over why it [&hellip;]<\/p>\n","protected":false},"author":9,"featured_media":10506,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3366],"tags":[8491,8489,7967,8492,8488,8490,7964,8487],"class_list":["post-10503","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-react","tag-react-beginner-tutorial","tag-react-functional-components","tag-react-hooks","tag-react-js-examples","tag-react-state-management","tag-update-state-in-react","tag-usestate-in-react","tag-usestate-in-react-js"],"_links":{"self":[{"href":"https:\/\/www.kaashivinfotech.com\/blog\/wp-json\/wp\/v2\/posts\/10503","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kaashivinfotech.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kaashivinfotech.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kaashivinfotech.com\/blog\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kaashivinfotech.com\/blog\/wp-json\/wp\/v2\/comments?post=10503"}],"version-history":[{"count":0,"href":"https:\/\/www.kaashivinfotech.com\/blog\/wp-json\/wp\/v2\/posts\/10503\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.kaashivinfotech.com\/blog\/wp-json\/wp\/v2\/media\/10506"}],"wp:attachment":[{"href":"https:\/\/www.kaashivinfotech.com\/blog\/wp-json\/wp\/v2\/media?parent=10503"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kaashivinfotech.com\/blog\/wp-json\/wp\/v2\/categories?post=10503"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kaashivinfotech.com\/blog\/wp-json\/wp\/v2\/tags?post=10503"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}