SkillPilotAI

useState: state in React

State is data a component remembers between renders, and changing it tells React to redraw the screen. useState is the hook that gives a component that memory; without it, a normal variable changes silently and the UI never updates.

26.1 The problem: a normal variable

  • Counter keeps a normal let count = 0.
  • handleClick adds 1 and logs it: the console shows 1, 2, 3.
  • The screen still shows Clicks: 0.
React (JSX)
function Counter() {  let count = 0;   function handleClick() {    count = count + 1;    console.log(count);  }   return (    <button onClick={handleClick}>      Clicks: {count}    </button>  );}
Output
-- after 3 clicks --[button] Clicks: 0Console: 1, 2, 3

26.2 Why didn't the number change?

  • React only redraws when it is told that data changed; changing a normal variable sends no signal.
  • Even if React re-rendered, the function runs again and let count = 0 resets it.
  • We need a value React remembers between renders and that notifies React when it changes: state.

26.3 Fix: useState

  • useState is a hook: a special React function that gives a component extra powers.
  • useState(0) takes the starting value and returns a pair: [count, setCount].
  • count is the current value; setCount is the only way to change it.
React (JSX)
import { useState } from "react"; function Counter() {  const [count, setCount] = useState(0);   function handleClick() {    setCount(count + 1);  }   return (    <button onClick={handleClick}>      Clicks: {count}    </button>  );}

26.4 What happens on click

  • handleClick calls setCount(count + 1): we do not change the value, we tell React the new value.
  • React runs the component again; this time useState returns 1 instead of 0.
  • The screen shows Clicks: 1, then 2 on the next click.
Output
-- start --[button] Clicks: 0-- click 1 --[button] Clicks: 1-- click 2 --[button] Clicks: 2
State change cycle
  1. 1.Click: handleClick runs

  2. 2.setCount(1): React is told

  3. 3.Re-render: Counter function runs again

  4. 4.useState returns the new value 1

  5. 5.New UI: Clicks: 1

Only this component and its children re-render, not the whole site

26.5 State is a snapshot

  • Inside one render, count is fixed.
  • Logging count right after setCount prints the old value; the new value arrives in the next render.
  • So the console shows 0 while the screen shows 1.
React (JSX)
function handleClick() {  setCount(count + 1);  console.log(count);}
Output
0Screen: Clicks 1

26.6 Updater function

  • Calling setCount(count + 1) three times in one click gives +1, not +3: count was 0 in all three calls (same snapshot).
  • Pass a function instead: setCount(c => c + 1). It receives the previous (queued) value, so the updates stack up to +3.
  • Rule: when the new value depends on the previous one, use the updater form.
React (JSX)
// Wrong: result is only +1function addThree() {  setCount(count + 1);  setCount(count + 1);  setCount(count + 1);}
React (JSX)
// Right: updater function, result is +3function addThree() {  setCount(c => c + 1);  setCount(c => c + 1);  setCount(c => c + 1);}
Value vs updater in one click
  1. 1.count = 0 in this render

  2. 2.setCount(count + 1) x3: all queue the value 1 -> result 1

  3. 3.setCount(c => c + 1) x3: 0 -> 1 -> 2 -> 3

  4. 4.React re-renders once with the final value

PointNormal variableuseState
Remembered between rendersNo, resets each renderYes
Change triggers re-renderNoYes, via the setter
How to changecount = count + 1setCount(count + 1) or setCount(c => c + 1)
Screen updatesNeverOn the next render

26.7 Common mistakes

  • Assigning state directly (count = 5): the value changes but React is not told, so the screen does not update. Always use the setter.
  • Expecting the new value right after setCount: it is a snapshot; use the updater or the next render.
  • Mutating object or array state: create a new copy instead (next module).

26.8 Rules of hooks

  1. Call useState at the top level of the component, never inside an if or a loop: React identifies hooks by their order.
  2. Hooks run only inside React components or custom hooks.
  3. Naming convention: value and setValue, like count and setCount.

26.9 Interview tips

  • useState is a hook that keeps a value in a component, remembered between renders.
  • It returns the value and a setter; calling the setter makes React re-render the component.
  • State is a snapshot in each render; the new value is visible in the next render.
  • When the next value depends on the previous one, use the updater function setX(prev => ...).
  • Hooks must be called at the top level because React tracks them by call order.