Full answer
State in React is not a variable that changes in place. Each render receives its own snapshot of state. If count is 0 in this render, then every use of count in this render's event handler is 0, even after calling the setter.
Look at three calls of setCount(count + 1). They are evaluated to setCount(1) three times, because count is still 0. React queues those updates and processes them together after the handler finishes, so the final state is 1.
An updater function changes the story. setCount((c) => c + 1) does not say "set to this value", it says "take the latest value and return the next one". React queues the three functions and runs them in order: 0 becomes 1, then 2, then 3. Use this form whenever the new state depends on the previous state, and in timers, intervals and async callbacks that could hold an old snapshot.
React batches updates. Since React 18, batching is automatic everywhere, including setTimeout callbacks, promises and native event handlers, not only React events. Several setState calls in one handler lead to one re-render, which is good for performance. If you need to read the DOM straight after a state change, flushSync from react-dom forces a synchronous update, but it is rarely necessary.
Two more details. Calling the setter with the same value, compared with Object.is, lets React skip the re-render. And the value you read right after calling the setter is still the old one, so do not log it expecting the new value.
import { useState } from 'react';
export default function Counters() {
const [direct, setDirect] = useState(0);
const [updater, setUpdater] = useState(0);
function addThree() {
setDirect(direct + 1);
setDirect(direct + 1);
setDirect(direct + 1);
setUpdater((n) => n + 1);
setUpdater((n) => n + 1);
setUpdater((n) => n + 1);
}
return (
<div>
<button onClick={addThree}>Add three times</button>
<p>Direct value: {direct}</p>
<p>Updater function: {updater}</p>
</div>
);
}One click calls each setter three times. Rendered result after the click: <div><button>Add three times</button><p>Direct value: 1</p><p>Updater function: 3</p></div>
Key points
- A render sees one fixed snapshot of state. Calling a setter does not change the variable in the current render.
- Use setX((previous) => next) when the next value depends on the previous one.
- Updates are batched into one re-render, including in timers and promises since React 18.
- Same-value updates are skipped using Object.is.
- flushSync forces an immediate update but is rarely needed.
Common mistakes
- Expecting the state variable to hold the new value on the next line after the setter call.
- Using a stale count inside setInterval or setTimeout instead of an updater function.
- Believing batching only happens inside React event handlers. That was true before React 18.
- Mutating an object in place and calling the setter with the same reference.
Follow-ups
- How would you read the new state value right after setting it?
- What changed about batching in React 18?
- What happens when you call a setter with the same value?
Tips
- Use the word "snapshot". It explains the behaviour in one step.
- Trace the example with numbers. Interviewers like to see the arithmetic.
- Mention automatic batching to show you know the 18 change.