React Reference ยท react-dom Client APIs
React automatic batching
Reference for React automatic batching: how several state updates in one event, timer or promise cause a single render, and when flushSync opts out.
Batching means React waits until the current piece of code has finished before it re-renders, so several set calls cause one render instead of several. Since React 18 (and therefore in React 19) this is automatic everywhere: inside event handlers, setTimeout callbacks, promise continuations, native event listeners and effects. There is nothing to import or turn on.
The rule of thumb: a state setter does not change the variable you already hold and does not render anything by itself. It schedules work. React processes everything that was scheduled during the same tick in one pass, and the screen only ever shows consistent combinations of state.
function handleClick() { setName('Ada'); setAge(36); setReady(true); // one render, with all three new values }
Try it
Open the console (it already shows one line from the first mount) and press each button. The first three print one committed line for three state changes. The flushSync button prints two lines, because flushSync forces a to be rendered before b is even scheduled.
The first button adds only 1 per click, because all three calls say "set to count + 1" using the same stale count. The second adds 3, because each updater function receives the result of the previous one.
Syntax
There is no API to call. Batching is a property of how the setters returned by useState and useReducer, root.render, and the dispatch of reducers behave.
// Opting OUT of batching for one update (rarely needed): import { flushSync } from 'react-dom'; flushSync(() => { setCount((n) => n + 1); }); // the DOM is already updated here
| Name | Description |
|---|---|
flushSync(callback) |
Runs callback, then renders and commits updates scheduled inside it synchronously before returning. See flushSync. |
unstable_batchedUpdates(fn) |
Exported by react-dom for old code. In React 18 and later it just calls fn; it adds nothing. |
Returns: nothing for the setters. Errors / edge cases: none, but a setter called with the value the state already has (compared with Object.is) may skip rendering entirely.
Usage notes
- What counts as "the same tick"? Everything that runs synchronously until control returns to the browser, plus updates queued from microtasks that run before React gets to flush. A
awaitsplits the work: updates before it and after it are separate batches. - Separate events are never merged. Two clicks are two renders, even if they are milliseconds apart. React only batches inside one event or task so each user action shows its result.
- Why it is safe. Because renders happen after your handler finishes, no component ever sees half-applied state, such as a new
firstNamewith an oldlastName. - The variable stays old.
const [count, setCount] = useState(0)gives a constant for the current render.setCount(5)does not changecountin the code that follows. Use an updater function when the next value depends on the previous one: State updater functions. - Before React 18 only updates inside React event handlers were batched. Code written for that era sometimes wraps work in
unstable_batchedUpdates; you can remove it. flushSynchurts performance. Use it only when you must read the updated DOM on the next line, for example to measure an element or move focus. Each call forces a render and can flush pending effects.- Transitions and actions are also batched, and they render at a lower priority; see
useTransition. - StrictMode renders twice in development, which can make a render counter look doubled. Count commits in an effect, as the first example does.