Skip to content
Tutorials Center
My learning

Topics

React Reference ยท Concepts and Pitfalls

State as a snapshot

Why a React state variable keeps the same value for the whole render and in every closure created by it, and what that means for handlers, timers and async code.

When React renders a component, it gives that render a snapshot of props and state. Every value you read, every event handler you create and every callback you pass along during that render sees exactly that snapshot, and it never changes afterwards. Setting state does not alter the variable you hold; it asks React to render again, and that next render gets a new snapshot with new variables.

This explains a common surprise: you call setCount(count + 1) and then log count, and you still see the old number. The variable count is a constant of the render you are in. The new value exists in the queue and will appear in the next render.

JSX
function handleClick() {
  setCount(count + 1);
  console.log(count); // still the old value
}

Try it

The first example logs count right after setting it, and again in the next render. Click once and compare the two lines.

The variable does not change in place

The second example shows that a handler remembers the snapshot it was created in. Click "Report in 1 second", then quickly click "Add one" a few times. The message reports the value from the moment of the click, not the latest one.

A delayed handler sees its own snapshot

Syntax

This is a mental model, not an API. The relevant facts:

Question Answer
When does a state variable get a new value? Only in the next render, as a new constant from useState.
What does setState do? Queues an update and schedules a render. It returns undefined.
What does a handler see? The props and state of the render that created it.
What does an updater function see? The most recent queued state, supplied by React when it processes the queue.
How do I read the "latest" value in async code? Use an updater function, a ref, or compute the next value once and use it for both the setter and later code.
JSX
function handleClick() {
  const next = count + 1;
  setCount(next);
  console.log(next); // the value you just asked for
}

Usage notes

  • State is not a mutable variable. Think of each render as a photograph of the UI: the JSX, the handlers and their values all belong together.
  • Props are snapshots too. If a parent re-renders with new props while an old handler is still waiting, that handler uses the old props.
  • This is a feature. A handler that started an operation with one value keeps using that value, so the code reads in order and a later change cannot silently alter an earlier one.
  • Asynchronous code is where it bites. Timers, promises and event listeners registered once can hold on to a very old snapshot. See Stale closures.
  • Objects and arrays follow the same rule. The state object in a snapshot should never be mutated; create a copy and pass it to the setter.
  • A re-render does not reset anything. The snapshot changes, but state values persist between renders because React stores them.
  • For the newest value inside an effect, use useEffectEvent or a ref instead of fighting the snapshot.