← Back to React, page 6

react-0106 · Easy fresher

What does this component log when the button is clicked, and why does console.log show the old state right after setState?

It logs the old value. State is a snapshot for each render: the handler runs with the count from the render that created it, and setCount schedules the next render but does not change the variable. The next render gets the new value. To use the new value immediately, compute it in a local variable, or read it in the next render or in an effect.

Full answer

A common beginner puzzle: after setCount(count + 1), console.log(count) prints the previous number. The reason is how state works.

Each render calls your component function once and gives it constant values: count is 0 for that call, like a local const. An event handler created in that render closes over that count. Calling setCount(1) does not mutate the variable. It asks React to render again later. The next call of the component receives count = 1, and the handlers created there see 1.

So inside the same handler, every read of count returns the same value, even after several setter calls, and even inside setTimeout created in that handler. This is why setCount(count + 1) called three times adds only one, and the functional form setCount((c) => c + 1) is used when each update depends on the previous.

How to get what you need:

  • If you need the new value now, compute it: const next = count + 1; setCount(next); console.log(next);.
  • To react to a state change, use the next render: put the code in the component body (derived values), or in an effect with the state in its dependencies.
  • For updates that depend on the previous state, use the updater function.

Related: React batches updates in a handler, so there is a single re-render after the handler finishes, not one per setter call.

The example logs inside the handler, in the render, and after a timer.

jsx
import { useState } from 'react';

export default function Counter() {
  const [count, setCount] = useState(0);
  console.log('render with count = ' + count);

  function handleClick() {
    setCount(count + 1);
    console.log('right after setCount(count + 1): ' + count);
    setCount(count + 1);
    setTimeout(() => console.log('in a timer started by the same click: ' + count), 0);
    const next = count + 1;
    console.log('local variable next: ' + next);
  }

  return <button onClick={handleClick}>Count: {count}</button>;
}

The check clicked once. The click called setCount twice with the same snapshot value, so the count went from 0 to 1, not 2. Output of the run: render with count = 0 right after setCount(count + 1): 0 local variable next: 1 render with count = 1 in a timer started by the same click: 0

Key points

  • State is a snapshot, constant for the whole render and for handlers created in it.
  • The setter schedules a re-render. It does not change the current variable.
  • Compute the next value in a local variable if you need it immediately.
  • Use updater functions when the new state depends on the previous state.
  • Updates in one handler are batched into one render.

Common mistakes

  • Expecting the variable to change right after calling the setter.
  • Calling setCount(count + 1) several times and expecting several increments.
  • Reading state in a timer and expecting the newest value.
  • Using an effect just to read the new value of a variable you can compute.
  • Mutating state to make the new value appear immediately.

Follow-ups

  • How does the updater form change the result?
  • Why does React batch these updates into one render?
  • How would you read the latest value inside a timer callback?

Tips

  • Say "snapshot" and compare it to a const in each render.
  • Give the local variable fix, then the updater fix.
  • Mention that the timer also sees the old value.