How it works
Trigger. In React 19.3.1, setState adds an update to the component's fiber, marks a lane (a priority) on it and its ancestors, and calls ensureRootIsScheduled, which queues one microtask per batch. In that microtask React decides what to do with each root: work in the sync lane (from discrete events like click and keydown) is rendered and committed right there; default and transition lanes are handed to React's Scheduler as separate tasks. All of this is implementation detail; the documented contract is only that updates are queued and batched.
Render. React walks the fiber tree from the root. Fibers with no pending work and no changed inputs are reused; components with pending updates are called, and their returned elements are reconciled against the existing child fibers. The result is a work-in-progress tree with flags recording which DOM operations and effects the commit needs. For transition updates, the loop checks between units of work whether to yield to the browser.
Commit. commitRoot runs in three passes over the flagged fibers:
- Before mutation: reads the DOM before it changes (
getSnapshotBeforeUpdate in class components).
- Mutation: inserts, updates and removes DOM nodes, detaches old refs and runs layout effect cleanups. React then makes the work-in-progress tree the current one (
root.current = finishedWork).
- Layout: attaches refs and runs
useLayoutEffect setups, componentDidMount and componentDidUpdate.
Passive effects (useEffect) come last. The source flushes them synchronously at the end of a commit that included the sync lane, "so that the result is immediately observable"; otherwise they wait until after paint. React 18 introduced that rule for discrete events.
Paint. The browser can only paint between tasks, after the microtask checkpoint, when a rendering opportunity arrives. Everything above, layout effects included, finishes inside one task, so the next frame shows the committed DOM.
// The Example's Counter, with this handler (flushSync from react-dom)
function handleClick() {
flushSync(() => setCount((c) => c + 1));
console.log('next line, DOM shows', button.current.textContent);
}
// → render 1
// → layout effect, DOM shows 1
// → effect
// → next line, DOM shows 1
Chrome 156 and Firefox 159, development build: flushSync ran the whole render and commit, effects included, before returning.
Edge cases
- Updates outside events. A
setState in a setTimeout callback uses the default lane. The handler's own microtask ran first, then React rendered in a Scheduler task. Chrome 156 rendered it before the next animation frame and Firefox 159 after it, so don't depend on either.
- Layout effect that sets state. The update is processed synchronously before paint, so a measured-then-corrected position never flashes; that's the documented use of
useLayoutEffect, at the cost of blocking paint.
- Same output. If a render returns elements equal in every prop to the last render, the commit finds nothing to change and the DOM isn't touched. Effects still run if their dependencies changed.
Real-world scenario
Adding an item to a list and scrolling to it: setItems(next) followed by listRef.current.lastChild.scrollIntoView() scrolls to the old last item, because the new <li> doesn't exist until commit. In the test, lastChild was still the old item after a plain setItems and the new one after flushSync. Either wrap the update in flushSync, or move the scroll into an effect that runs after the commit. The tooltip case is the reverse problem: measure in useLayoutEffect and set the position there, so the user never sees the first, unmeasured render.
Trade-offs
Separating render from commit is what lets React batch updates, pre-render work in the background, retry after Suspense, and double-render in Strict Mode, all without visible half-states. The cost is a rule: render must be pure, since React may call it any number of times. And by default an update re-renders the whole subtree below the component that changed; memo and the React Compiler are the opt-outs, not the commit phase.