{"id":61,"date":"2026-07-04T10:10:04","date_gmt":"2026-07-04T10:10:04","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/react\/04-react-state-usestate\/"},"modified":"2026-07-04T10:14:10","modified_gmt":"2026-07-04T10:14:10","slug":"04-react-state-usestate","status":"publish","type":"post","link":"https:\/\/www.kindsonthegenius.com\/react\/04-react-state-usestate\/","title":{"rendered":"React State and useState \u2014 Interactive UI (React 19+)"},"content":{"rendered":"<p><!-- ktg-updated-banner --><\/p>\n<div class=\"ktg-updated-banner\" style=\"margin:0 0 1.25em;padding:0.75em 1em;background:#fefce8;border-left:4px solid #ca8a04;border-radius:4px;\">\n<p><strong>Updated July 4, 2026.<\/strong> Refreshed for React 19 and current SEO best practices.<\/p>\n<\/div>\n<p><!-- ktg-series-toc --><\/p>\n<nav class=\"ktg-series-toc\" aria-label=\"React tutorial table of contents\" style=\"margin:1.5em 0;padding:1em 1.25em;background:#f8fafc;border:1px solid #e2e8f0;border-radius:8px;\">\n<h2>Table of Contents<\/h2>\n<p>Follow the React tutorial series in order:<\/p>\n<ol>\n<li><a href=\"https:\/\/www.kindsonthegenius.com\/react\/01-react-introduction\/\">Lesson 1: React Tutorial \u2014 Introduction (2026)<\/a><\/li>\n<li><a href=\"https:\/\/www.kindsonthegenius.com\/react\/02-react-setup\/\">Lesson 2: React Setup \u2014 Create Your First App with Vite<\/a><\/li>\n<li><a href=\"https:\/\/www.kindsonthegenius.com\/react\/03-react-jsx-and-components\/\">Lesson 3: React JSX and Components \u2014 Build Your First UI<\/a><\/li>\n<li><strong aria-current=\"page\">Lesson 4: React State and useState \u2014 Interactive UI (React 19+)<\/strong><\/li>\n<li><a href=\"https:\/\/www.kindsonthegenius.com\/react\/05-react-useeffect\/\">Lesson 5: React useEffect \u2014 Side Effects and Data Fetching (React 19+)<\/a><\/li>\n<li><a href=\"https:\/\/www.kindsonthegenius.com\/react\/06-react-forms\/\">Lesson 6: React Forms \u2014 Controlled Inputs and Validation (React 19+)<\/a><\/li>\n<li><a href=\"https:\/\/www.kindsonthegenius.com\/react\/07-react-router\/\">Lesson 7: React Router \u2014 Multi-Page Navigation (React 19+)<\/a><\/li>\n<li><a href=\"https:\/\/www.kindsonthegenius.com\/react\/\">\u2190 Back to React Tutorial hub<\/a><\/li>\n<\/ol>\n<\/nav>\n<p><!-- ktg-react-modern --><\/p>\n<div class=\"ktg-react-modern\" style=\"margin:1.5em 0;padding:1em;background:#eff6ff;border-left:4px solid #2563eb;border-radius:4px;\">\n<h4>React 19 Notes<\/h4>\n<p>React 19 batches state updates for performance. Prefer functional updaters <code>setState(s =&gt; ...)<\/code> when the next value depends on the previous one. The React Compiler can optimize many components automatically \u2014 still follow the Rules of Hooks.<\/p>\n<\/div>\n<p>This lesson covers <strong>React state<\/strong> and the <code>useState<\/code> hook \u2014 how components remember data that changes over time and re-render when that data updates. You will handle events, update state safely, and build a small counter and like-button example.<\/p>\n<p><strong>Prerequisites:<\/strong> Lessons 1\u20133 (<a href=\"https:\/\/www.kindsonthegenius.com\/react\/01-react-introduction\/\">Introduction<\/a>, <a href=\"https:\/\/www.kindsonthegenius.com\/react\/02-react-setup\/\">Setup<\/a>, <a href=\"https:\/\/www.kindsonthegenius.com\/react\/03-react-jsx-and-components\/\">JSX and Components<\/a>) with a Vite React project. <strong>Estimated time:<\/strong> 45\u201355 minutes.<\/p>\n<h4 id=\"props-vs-state\">1. Props vs State<\/h4>\n<p><strong>Props<\/strong> flow down from parent to child and never change inside the child. <strong>State<\/strong> is private data owned by a component \u2014 when state changes, React re-renders that component and its children with the new UI.<\/p>\n<p>Rule of thumb: if data should change because the user clicked, typed, or a timer fired, it belongs in state (or a state library later). If data is configuration from a parent, use props.<\/p>\n<figure style=\"margin:1.5em 0;text-align:center;\"><img decoding=\"async\" src=\"https:\/\/www.kindsonthegenius.com\/react\/wp-content\/uploads\/2026\/07\/04-react-state-usestate-section-1.jpg\" alt=\"Interactive React UI with state\" width=\"1200\" height=\"675\" loading=\"lazy\" style=\"max-width:100%;height:auto;border-radius:8px;border:1px solid #dee2e6;\" \/><figcaption style=\"font-size:0.85em;color:#666;margin-top:0.5em;\">Photo by <a href=\"https:\/\/unsplash.com\/@joshuaryanphoto?utm_source=kindsonthegenius&#038;utm_medium=referral\" rel=\"noopener noreferrer\" target=\"_blank\">Joshua Reddekopp<\/a> on <a href=\"https:\/\/unsplash.com\/?utm_source=kindsonthegenius&#038;utm_medium=referral\" rel=\"noopener noreferrer\" target=\"_blank\">Unsplash<\/a><\/figcaption><\/figure>\n<h4 id=\"usestate-basics\">2. The useState Hook<\/h4>\n<p>Import and call <code>useState<\/code> at the top of a function component:<\/p>\n<pre><code class=\"language-jsx\">import { useState } from 'react';\n\nfunction Counter() {\n  const [count, setCount] = useState(0);\n\n  return (\n    &lt;div&gt;\n      &lt;p&gt;Count: {count}&lt;\/p&gt;\n      &lt;button onClick={() =&gt; setCount(count + 1)}&gt;\n        Increment\n      &lt;\/button&gt;\n    &lt;\/div&gt;\n  );\n}\n\nexport default Counter;\n<\/code><\/pre>\n<p><code>useState(initialValue)<\/code> returns a pair: the current value and a setter function. Always update state with the setter \u2014 never assign directly to <code>count<\/code>.<\/p>\n<figure style=\"margin:1.5em 0;text-align:center;\"><img decoding=\"async\" src=\"https:\/\/www.kindsonthegenius.com\/react\/wp-content\/uploads\/2026\/07\/04-react-state-usestate-section-2.jpg\" alt=\"Developer building React stateful components\" width=\"1200\" height=\"675\" loading=\"lazy\" style=\"max-width:100%;height:auto;border-radius:8px;border:1px solid #dee2e6;\" \/><figcaption style=\"font-size:0.85em;color:#666;margin-top:0.5em;\">Photo by <a href=\"https:\/\/unsplash.com\/@florianolv?utm_source=kindsonthegenius&#038;utm_medium=referral\" rel=\"noopener noreferrer\" target=\"_blank\">Florian Olivo<\/a> on <a href=\"https:\/\/unsplash.com\/?utm_source=kindsonthegenius&#038;utm_medium=referral\" rel=\"noopener noreferrer\" target=\"_blank\">Unsplash<\/a><\/figcaption><\/figure>\n<h4 id=\"events\">3. Handling Events<\/h4>\n<p>React uses camelCase event names and passes functions, not strings:<\/p>\n<pre><code class=\"language-jsx\">&lt;button onClick={handleClick}&gt;Click me&lt;\/button&gt;\n&lt;input onChange={(e) =&gt; setName(e.target.value)} \/&gt;\n<\/code><\/pre>\n<p>Common events: <code>onClick<\/code>, <code>onChange<\/code>, <code>onSubmit<\/code>, <code>onKeyDown<\/code>. Prevent default form behavior with <code>e.preventDefault()<\/code> inside <code>onSubmit<\/code>.<\/p>\n<h4 id=\"functional-updates\">4. Functional State Updates<\/h4>\n<p>When the next state depends on the previous state, pass a function to the setter:<\/p>\n<pre><code class=\"language-jsx\">setCount((prev) =&gt; prev + 1);\n<\/code><\/pre>\n<p>This avoids stale closures when multiple updates happen in one event or in async code \u2014 a common <strong>react usestate tutorial<\/strong> gotcha.<\/p>\n<h4 id=\"multiple-state\">5. Multiple State Variables<\/h4>\n<p>Split unrelated data into separate <code>useState<\/code> calls:<\/p>\n<pre><code class=\"language-jsx\">const [likes, setLikes] = useState(0);\nconst [liked, setLiked] = useState(false);\n<\/code><\/pre>\n<p>For complex objects, you can use one state object, but prefer separate variables until you need to group them (or use <code>useReducer<\/code> in advanced apps).<\/p>\n<h4 id=\"like-button-example\">6. Complete Example \u2014 LikeButton<\/h4>\n<p>Create <code>src\/LikeButton.jsx<\/code>:<\/p>\n<pre><code class=\"language-jsx\">import { useState } from 'react';\n\nfunction LikeButton({ initialLikes = 0 }) {\n  const [likes, setLikes] = useState(initialLikes);\n  const [liked, setLiked] = useState(false);\n\n  function toggleLike() {\n    setLiked((prev) =&gt; !prev);\n    setLikes((prev) =&gt; (liked ? prev - 1 : prev + 1));\n  }\n\n  return (\n    &lt;button\n      onClick={toggleLike}\n      style={{\n        padding: '0.5rem 1rem',\n        borderRadius: '6px',\n        border: '1px solid #e2e8f0',\n        background: liked ? '#eff6ff' : '#fff',\n        cursor: 'pointer',\n      }}\n    &gt;\n      {liked ? '\u2665' : '\u2661'} {likes} likes\n    &lt;\/button&gt;\n  );\n}\n\nexport default LikeButton;\n<\/code><\/pre>\n<p>Wire it in <code>App.jsx<\/code> alongside your Lesson 3 components. Run <code>npm run dev<\/code> and confirm the count updates on each click.<\/p>\n<h4 id=\"rules-of-hooks\">7. Rules of Hooks<\/h4>\n<ul>\n<li>Only call hooks at the <strong>top level<\/strong> of a function component \u2014 not inside loops, conditions, or nested functions<\/li>\n<li>Only call hooks from React function components or custom hooks<\/li>\n<li>Hook names start with <code>use<\/code> by convention<\/li>\n<\/ul>\n<p>React 19&#8217;s compiler can optimize many patterns, but these rules still apply to <code>useState<\/code>, <code>useEffect<\/code>, and other hooks.<\/p>\n<h4 id=\"next\">8. Next Steps<\/h4>\n<p>State makes UIs interactive. Next you will learn <strong>useEffect<\/strong> for side effects such as fetching data, subscribing to events, and syncing with the browser API.<\/p>\n<p>Series: <a href=\"https:\/\/www.kindsonthegenius.com\/react\/03-react-jsx-and-components\/\">Lesson 3<\/a> \u00b7 <a href=\"https:\/\/www.kindsonthegenius.com\/react\/05-react-useeffect\/\">Lesson 5 \u2014 useEffect<\/a><\/p>\n<h4 id=\"faq\">Frequently Asked Questions<\/h4>\n<p><strong>What is useState in React?<\/strong><br \/>\n<code>useState<\/code> is a hook that adds local state to a function component. It returns the current value and a function to update it, triggering a re-render.<\/p>\n<p><strong>Why does my React state not update immediately?<\/strong><br \/>\nState updates are asynchronous and batched. Use the functional setter form <code>setCount(c =&gt; c + 1)<\/code> when the next value depends on the previous one.<\/p>\n<p><strong>Can I use useState for objects and arrays?<\/strong><br \/>\nYes. When updating objects or arrays, create a new copy (spread or <code>map<\/code>) instead of mutating the existing value \u2014 React compares by reference.<\/p>\n<p><!-- ktg-faq-schema --><br \/>\n<script type=\"application\/ld+json\">{\"@context\":\"https:\/\/schema.org\",\"@type\":\"FAQPage\",\"mainEntity\":[{\"@type\":\"Question\",\"name\":\"What is useState in React?\",\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"useState is a hook that adds local state to a function component. It returns the current value and a setter function that triggers a re-render when called.\"}},{\"@type\":\"Question\",\"name\":\"Why does React state not update immediately?\",\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"State updates are asynchronous and batched. Use the functional setter setCount(c => c + 1) when the next value depends on the previous one.\"}}]}<\/script><\/p>\n<p><!-- ktg-lesson-nav --><\/p>\n<nav class=\"ktg-lesson-nav\" aria-label=\"Lesson navigation\">\n<p><strong>Previous:<\/strong> <a href=\"https:\/\/www.kindsonthegenius.com\/react\/03-react-jsx-and-components\/\">Lesson 3: React JSX and Components \u2014 Build Your First UI<\/a><\/p>\n<p><strong>Next:<\/strong> <a href=\"https:\/\/www.kindsonthegenius.com\/react\/05-react-useeffect\/\">Lesson 5: React useEffect \u2014 Side Effects and Data Fetching (React 19+)<\/a><\/p>\n<\/nav>\n<p><!-- ktg-alkademy-cta --><\/p>\n<div class=\"ktg-alkademy-cta\">\n<p><strong>Want live React or frontend classes?<\/strong> Join <a href=\"https:\/\/www.alkademy.com\/courses\" target=\"_blank\" rel=\"noopener noreferrer\">Alkademy<\/a> for instructor-led React and JavaScript courses with hands-on projects.<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Updated July 4, 2026. Refreshed for React 19 and current SEO best practices. Table of Contents Follow the React tutorial series in order: Lesson 1: React Tutorial \u2014 Introduction (2026) Lesson 2: React Setup \u2014 Create Your First App with Vite Lesson 3: React JSX and Components \u2014 Build Your First UI Lesson 4: React [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":58,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"pagelayer_contact_templates":[],"_pagelayer_content":"","footnotes":""},"categories":[2],"tags":[],"class_list":["post-61","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-react-tutorials"],"_links":{"self":[{"href":"https:\/\/www.kindsonthegenius.com\/react\/wp-json\/wp\/v2\/posts\/61","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kindsonthegenius.com\/react\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kindsonthegenius.com\/react\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/react\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/react\/wp-json\/wp\/v2\/comments?post=61"}],"version-history":[{"count":3,"href":"https:\/\/www.kindsonthegenius.com\/react\/wp-json\/wp\/v2\/posts\/61\/revisions"}],"predecessor-version":[{"id":89,"href":"https:\/\/www.kindsonthegenius.com\/react\/wp-json\/wp\/v2\/posts\/61\/revisions\/89"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/react\/wp-json\/wp\/v2\/media\/58"}],"wp:attachment":[{"href":"https:\/\/www.kindsonthegenius.com\/react\/wp-json\/wp\/v2\/media?parent=61"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/react\/wp-json\/wp\/v2\/categories?post=61"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/react\/wp-json\/wp\/v2\/tags?post=61"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}