Skip to content
Tutorials Center
My learning

Topics

React Reference ยท Concepts and Pitfalls

State batching

How React batches state updates into a single render, why setCount(count + 1) three times adds only one, how updater functions fix it and when flushSync opts out.

Calling a state setter does not change the variable and does not render immediately. It puts an update in a queue. When your event handler (or any other code that is running) finishes, React processes the whole queue and renders once with the final state. This is called batching.

Batching makes UIs faster, because three set calls cost one render instead of three, and it keeps the screen consistent, because no one ever sees a half-updated state. Since React 18 it applies everywhere: event handlers, timeouts, promises and native event listeners.

JSX
function handleClick() {
  setA(1);
  setB(2);
  setC(3);
  // no render has happened yet; one render follows after this function returns
}

Try it

Each button below calls the setter three times. The first uses the value count from this render three times, so the queue holds "set to 1" three times and the result is 1. The second uses an updater function, which receives the latest queued value, so the result is 3. In both cases only one render happens per click, as the console shows.

Three updates, one render

Updates in asynchronous code are batched too. To see updates applied one at a time you have to opt out with flushSync from react-dom, which forces a render for the updates made inside it.

Batching in a timeout, and flushSync

The batched button logs one render line, the other logs two.

Syntax

There is no batching API in current React. The relevant pieces are:

TypeScript
setState(value);             // queue "replace state with value"
setState((prev) => next);    // queue "compute the next state from the latest queued one"
flushSync(() => { /* updates */ }); // from "react-dom": render synchronously
Piece Description
value form The state becomes value. If you use a variable such as count in the expression, it holds the value from the render that created the handler.
updater form React calls your function during the next render, passing the state as it stands after all earlier queued updates. It must be pure and return the new state.
flushSync(fn) Runs fn and renders the resulting updates synchronously before returning.

Usage notes

  • Use the updater form whenever the new state depends on the previous one and you may update more than once before the next render (counters, toggles in loops, appending to arrays).
  • Updates apply in order. setN(5); setN(n => n + 1) ends at 6. setN(n => n + 1); setN(5) ends at 5.
  • Different state variables are batched together, so related setters never produce a render where only one of them has changed.
  • React 17 and earlier batched only inside React event handlers; updates in timeouts and promises rendered one by one. React 18 batches all of them. ReactDOM.render is gone in React 19, so legacy behavior no longer exists.
  • Do not reach for flushSync casually. It hurts performance. Its legitimate use is reading the updated DOM immediately afterwards, for example scrolling to a newly added item.
  • Reading state right after calling a setter returns the old value; see State as a snapshot.
  • Batching spans many renders in transitions, where React may interleave work; see Concurrent rendering.