How it works
React 19.3.1 stores each state hook's pending updates as a circular linked list (queue.pending) on the hook; each update records its action (the value or updater) and its lane, the priority it was scheduled with (implementation details from ReactFiberHooks.js). The setter, dispatchSetState, appends an update and calls scheduleUpdateOnFiber. Scheduling is per root, not per update: the first update queues one microtask, and later updates find the render already scheduled. That's the whole batching mechanism, and it's why the context, whether a click, a timeout or a promise, no longer matters.
When the component renders, useState runs the queue through a reducer that is literally typeof action === 'function' ? action(state) : action. A value ignores the state it's given; an updater uses it. Updates whose lane isn't part of this render, such as a transition while an urgent update renders, are skipped and kept, then re-applied in their original order in a later render, so the final state is the same as if everything had run in order.
Render counts with two setters per callback, development and production, Chrome 156 and Firefox 159:
function handle() {
setA((x) => x + 1);
setB((x) => x + 1);
}
<button onClick={handle} />; // → 1 render
<button onClick={() => setTimeout(handle, 0)} />; // → 1 render
<button onClick={() => Promise.resolve().then(handle)} />; // → 1 render
useEffect(() => { button.current.addEventListener('click', handle); }, []); // → 1 render
A synthetic button.click() from script behaved the same as a real click: one render.
Edge cases
When updaters actually run. The docs say updaters run during the next render. React 19.3.1 adds an optimization in dispatchSetState: when the component has no pending update, it computes the first update immediately, at the setter call, so it can drop updates that don't change the state. Later updates wait for the render:
function handleClick() {
setN((m) => { console.log('updater 1 gets', m); return m + 1; });
setN((m) => { console.log('updater 2 gets', m); return m + 1; });
console.log('handler done');
}
// First click after mount:
// → updater 1 gets 0
// → handler done
// → updater 2 gets 1
// → render 2
// Second click:
// → handler done
// → updater 1 gets 2
// → updater 2 gets 3
// → render 4
Chrome 156 and Firefox 159, development and production, no Strict Mode. On the second click the earlier render still left work marked on the fiber's other copy, so nothing ran early. Under Strict Mode in development each updater also ran once more during render, its result discarded. Code that is pure can't tell any of this apart, which is the point.
flushSync splits a batch.
// The component logs 'render', a, b and renders <span ref={span} data-a={a} />
function handleClick() {
flushSync(() => setA((x) => x + 1));
console.log('DOM shows', span.current.dataset.a);
setB((x) => x + 1);
}
// → render 1 0
// → DOM shows 1
// → render 1 1
Development and production, both browsers. Per the docs, flushSync may also flush other pending updates and effects first, and can force a Suspense fallback to show.
- Render-phase updates. Calling a setter during render is allowed only for the component that's rendering; React discards the output and re-renders immediately. That's the "store information from previous renders" pattern, not batching.
- Same-value updates. If the queue's result equals the current state by
Object.is, React skips the children; see the re-render question.
Real-world scenario
A "like" button calls setLikes(likes + 1) optimistically and, if the request fails, rolls back with setLikes(likes). Starting from 0, the user clicks twice and then the first request fails: its rollback restores the first click's snapshot, 0, wiping out the second like too. With setLikes(l => l + 1) and a rollback of setLikes(l => l - 1), the count ends at 1, because each change applies to the state as React finds it. The same reasoning applies to anything driven by intervals, sockets or retries, where the callback outlives the render that created it.
Trade-offs
Batching trades immediacy for consistency and speed: one render per event instead of one per setter, and no half-updated UI where one state variable changed and another didn't yet. The cost is that nothing is observable until React renders, which surprises code written for synchronous this.setState behaviour and for direct DOM reads. Updaters add a little indirection; the docs treat always using them for derived updates as a reasonable style, not a requirement. When several state variables must change together by rules, a reducer usually reads better than a chain of updaters.