Calling setX(next) doesn't change x immediately. It enqueues an update on the component's fiber (a value or an updater function), marks that fiber and its ancestors as needing work at a priority lane (urgent for a click, lower inside startTransition), and asks React to schedule a render. Updates from the same tick are batched (automatically everywhere since React 18). During the next render React processes the queue in order to compute the new state, re-renders the component and its children, reconciles, and commits the DOM changes. Then effects run. If the new state is Object.is-equal to the current one, React may bail out and skip re-rendering.
function Counter() {
const [count, setCount] = useState(0);
function handleClick() {
setCount(count + 1);
setCount(c => c + 1);
console.log(count); // 0: this render's snapshot
}
return <button onClick={handleClick}>{count}</button>;
}
createRoot extends batching to promises, setTimeout and native event handlers. Before React 18, only React event handlers batched.Counter again. useState walks the queue: 0 → 1 (replace) → 2 (updater). The new count is 2.The console.log printed 0, because state is a snapshot per render. The handler closed over the render where count was 0.
setCount(0); // when count is already 0
If the queue was empty, React can compare eagerly and skip scheduling a render entirely. Otherwise it may still render the component once to confirm, but if the result is equal it won't re-render the children or commit. Objects are compared by reference, so setUser(user) after mutating it is a no-op. That's why mutation breaks updates.
startTransition get a transition lane. React renders them in the background, can interrupt them for urgent updates, and keeps the old UI on screen meanwhile.flushSync(() => setX(…)) forces a synchronous render and commit, which you need in rare cases like measuring the DOM right after an update. Use it sparingly.console.log shows the old value.flushSync as priority controls.Object.is bail-out, and why mutation defeats it.Spotted a mistake or a rights concern? Report an issue