
React’s setState Doesn’t Update Immediately — Why Two count + 1 Calls Still Add Only 1
You click a button once. The handler calls setCount(count + 1) twice. You expect the counter to jump by 2. It jumps by 1. Here is the exact reason, and the one-line fix that makes it correct.
The surprising snippet
function Counter() {
const [count, setCount] = useState(0);
const handleClick = () => {
setCount(count + 1); // schedules: count 0 -> 1
setCount(count + 1); // schedules: count 0 -> 1 (same value!)
};
return <button onClick={handleClick}>Clicked {count} times</button>;
}
Click once → count becomes 1, not 2.
Why it happens
- State is a snapshot. On every render,
countis a plain number captured from that render. InsidehandleClick, both lines read the samecount— the value from the render in which the handler was created. - Updates are batched. React 18 batches all
setStatecalls inside the same event handler into a single re-render. So React only sees two requests to set the state to1, and the last one wins — there is no intermediatecount = 1between them. - Result:
0 → 1, then0 → 1again. Net effect:+1.
This is not a bug. Batching is a deliberate performance optimization — without it, one click could trigger two full re-renders.
The fix: a functional updater
Pass a function instead of a value. React gives that function the latest queued state, so each call builds on the previous one:
const handleClick = () => {
setCount(c => c + 1); // 0 -> 1
setCount(c => c + 1); // 1 -> 2
};
Now a single click adds 2. The arrow function receives the real current state at apply time, sidestepping the stale snapshot entirely.
It’s not just click handlers
The same trap bites anywhere the value is captured before the update applies:
// Bug: each timeout closes over the same stale count -> 3 calls add only +1
for (let i = 0; i < 3; i++) {
setTimeout(() => setCount(count + 1), 0);
}
// Correct: each timeout builds on the queued state -> +3
for (let i = 0; i < 3; i++) {
setTimeout(() => setCount(c => c + 1), 0);
}
In async code (await, Promise.then, setTimeout) React 18 still batches, but the captured count is whatever it was when that callback closed over it — so the functional updater is the only reliable way to increment.
A second subtle trap: reading state right after you set it
const handleClick = () => {
setCount(count + 1);
console.log(count); // still the OLD value — re-render has not happened yet
};
setState is asynchronous from your code’s perspective. If you need the next value for a calculation, compute it from the updater or derive it during render — never from the variable you just set.
Takeaways
setStateschedules an update; it does not mutate state synchronously.- In a handler,
count + 1reads a stale snapshot — calling it twice adds 1, not 2. - Use the functional form
setCount(c => c + 1)whenever the next state depends on the previous one (counters, toggles, accumulation, async flows). - Read “the new value” from render output, not from the variable right after the setter call.