Catalog My learning Blog
Frontend Developer Course: 100 Concepts in 100 Days · Phase 6 · Lesson 53 · 5 min read

React useState and Re-Renders Explained

On this page
  1. Learning Objective
  2. Concept Explanation
  3. Worked Example: Snapshots, Updaters and Immutable Updates
  4. Real-World Examples
  5. Review Questions & Answers

Learning Objective

Explain how state updates trigger re-renders, why state behaves as a snapshot within one render, how updates are batched and when to use updater functions, why state must be replaced rather than mutated, and how React decides which components re-render.

Concept Explanation

State is a component's memory: data that persists between renders and changes over time in response to interaction. In React you declare it with useState, which returns the current value and a setter. Calling the setter does not change the variable you already have. It schedules a re-render, and on that next render React calls your component function again and useState returns the new value. Every render is therefore a complete snapshot: its props, its state and every handler created during it see the values of that render.

The snapshot model explains a classic surprise. Inside a click handler, calling setCount(count + 1) three times does not add three, because count is the same snapshot value in all three calls; each one asks for the same next value. When the next state depends on the previous one, pass an updater function, setCount((c) => c + 1). React queues updaters and applies them in order, each receiving the result of the previous one, so three updater calls add three. Reading the state variable right after calling the setter also still gives the old value, for the same reason.

React batches state updates. All updates made during one event handler, and since React 18 also in timeouts, promises and native event handlers, are collected and processed in a single re-render after the handler finishes. This avoids rendering intermediate states and keeps performance predictable. The render itself has two parts: React calls components to produce a new element tree, then commits the differences to the DOM. Effects run after the commit (Day 54).

State must be treated as immutable, exactly as Day 39 described. React decides whether to re-render by comparing the new state with the old using Object.is. If you mutate an array or object and pass the same reference to the setter, React sees no change and may skip the update; even when something else causes a render, mutated state makes behavior unpredictable. Always create a new object or array with the change.

When a component re-renders, React also re-renders its children by default, because their props may have changed. That is usually fast, since rendering only produces a description and the DOM changes only where output differs. Where it is not fast enough, memoization lets children skip rendering when their props are unchanged (Day 89). Where state lives decides how much re-renders: state held high in the tree re-renders a large subtree on every change, so state should live as close as possible to the components that use it, a principle called colocation. The React Compiler, released in 2025, can memoize automatically, but the mental model of snapshots and re-renders remains the same.

ConceptBehaviorPractical rule
useState setterSchedules a re-renderDoes not change the current variable
SnapshotEach render sees its own valuesHandlers read the render's state
Updater functionsetCount(c => c + 1)Use when next state depends on previous
BatchingUpdates in one event render onceNo intermediate renders
Object.is comparisonSame reference means no changeNever mutate state
Child re-rendersChildren render with the parentUsually cheap, memoize when measured
ColocationState near its usersSmaller re-render scope

Worked Example: Snapshots, Updaters and Immutable Updates

The counter shows why repeated setter calls with a snapshot value do not add up. The tag editor shows the mutation bug and the immutable fix.

TypeScript
function Counter() {
  const [count, setCount] = useState(0);

  function addThreeBroken() {
    setCount(count + 1);
    setCount(count + 1);
    setCount(count + 1);          // all three request 0 + 1: result is 1
    console.log(count);           // still 0: this render's snapshot
  }

  function addThree() {
    setCount((c) => c + 1);
    setCount((c) => c + 1);
    setCount((c) => c + 1);       // queued updaters: 0 -> 1 -> 2 -> 3, one re-render
  }

  return (
    <>
      <p>{count}</p>
      <button onClick={addThreeBroken}>+3 (broken)</button>
      <button onClick={addThree}>+3</button>
    </>
  );
}

function TagEditor() {
  const [tags, setTags] = useState(['react']);

  function addTagBroken(tag) {
    tags.push(tag);               // mutation: same array reference
    setTags(tags);                // Object.is(old, new) is true: the update can be skipped
  }

  function addTag(tag) {
    setTags((prev) => (prev.includes(tag) ? prev : [...prev, tag]));   // new array
  }

  function removeTag(tag) {
    setTags((prev) => prev.filter((t) => t !== tag));
  }

  return (
    <ul>
      {tags.map((t) => <li key={t}>{t} <button onClick={() => removeTag(t)} aria-label={`Remove ${t}`}>x</button></li>)}
      <li><button onClick={() => addTag('typescript')}>Add typescript</button></li>
    </ul>
  );
}

Open React DevTools with "Highlight updates when components render" enabled: clicking +3 produces one highlighted render showing 3, which demonstrates both the updater queue and batching.

Every render sees fixed values, so three calls of setCount(count + 1) all request 0 + 1. Updater functions are queued and each receives the latest pending value, so three calls produce 3, all in one batched render.
Every render sees fixed values, so three calls of setCount(count + 1) all request 0 + 1. Updater functions are queued and each receives the latest pending value, so three calls produce 3, all in one batched render.

Real-World Examples

  • React 18 introduced automatic batching for updates inside promises, timeouts and native event handlers, which previously caused one render per setState call, and the React team's announcement highlighted it as a free performance improvement for existing apps.
  • React Compiler 1.0, released in 2025, automatically memoizes components and values based on static analysis, and its documentation notes that it relies on components following the rules of React, pure rendering and immutable state, to do so safely.
Key TakeawaysSetting state in React schedules a re-render, and each render is a snapshot whose event handlers see the values from that particular render. Use updater functions when the next state depends on the previous one, rely on batching to group updates, and always replace state rather than mutating it, because changes are detected by reference. Colocate state near its users to keep re-renders small.

Review Questions & Answers

count is a constant snapshot for the current render, so all three calls request the same value, the old count plus one. Using the updater form, setCount(c => c + 1), queues functions that each receive the latest pending value, so three calls add three.

The setter does not change the variable in the current render; it schedules a new render in which useState returns the new value. The current function keeps running with its snapshot.

React collects all state updates made during one event or task and processes them in a single re-render afterward, instead of rendering after each update. This avoids wasted renders and showing intermediate states.

React compares the old and new state with Object.is. Mutating the existing array and passing it to the setter gives the same reference, so React considers the state unchanged and can skip rendering, leaving the screen out of date.