Full answer
React splits an update into two phases.
Render phase (also called reconciliation). React calls your function components (and class render methods), compares the result with the previous tree, and marks what must change. No DOM is touched. In concurrent mode this phase can be interrupted, restarted, or thrown away, which is why render must be free of side effects. Component bodies run here, including useMemo and useState initializers.
Commit phase. React applies the result to the DOM in one synchronous pass, which cannot be paused, so users never see a half-updated screen. It has sub-steps:
- Before mutation: reads from the DOM (class
getSnapshotBeforeUpdate). - Mutation: inserts, updates and removes DOM nodes. Cleanups of layout effects and detaching of old refs happen here.
- Layout: refs are attached and
useLayoutEffectcallbacks (and classcomponentDidMountandcomponentDidUpdate) run. The DOM is updated, but the browser has not painted yet, so you can measure withgetBoundingClientRectand update state synchronously to avoid flicker. - Passive effects:
useEffectcleanups and callbacks run after the browser has had a chance to paint, so they do not delay visuals. When an update was caused by a discrete event such as a click, React may flush passive effects synchronously before the next render to keep state consistent.
Ordering inside a phase follows the tree: children complete before parents, so a child's layout effect and ref attach run before its parent's layout effect.
Implications. Do measuring and DOM synchronization in layout effects, data fetching and subscriptions in passive effects. Never perform side effects in the render phase. Refs are not set during render, so you can only read ref.current in effects and handlers. Strict Mode adds a render, and an extra mount, unmount, mount of effects in development.
The example logs every step for a mount and one update.
import { useEffect, useLayoutEffect, useRef, useState } from 'react';
export default function Box() {
const [count, setCount] = useState(0);
const buttonRef = useRef(null);
console.log('render phase: count=' + count);
useLayoutEffect(() => {
console.log('layout effect: DOM text is "' + buttonRef.current.textContent + '"');
return () => console.log('layout effect cleanup');
});
useEffect(() => {
console.log('passive effect: count=' + count);
return () => console.log('passive effect cleanup: count=' + count);
});
return (
<button ref={buttonRef} onClick={() => setCount(count + 1)}>
Clicked {count}
</button>
);
}The log shows the mount first and then the update after the click. Note that the layout effect already sees the new DOM text. On the update, the old layout cleanup runs before the new layout effect, and the old passive cleanup runs after the layout effect, just before the new passive effect. Output of the run: render phase: count=0 layout effect: DOM text is "Clicked 0" passive effect: count=0 render phase: count=1 layout effect cleanup layout effect: DOM text is "Clicked 1" passive effect cleanup: count=0 passive effect: count=1 layout effect cleanup passive effect cleanup: count=1
Key points
- Render computes the new tree, is interruptible and must be pure. Commit applies it synchronously.
- Commit order: mutate the DOM, attach refs and run layout effects, then run passive effects after paint.
- Layout effects can measure the DOM before paint. Passive effects do not block painting.
- Children complete before parents in the same phase.
- Refs are not available during render. Read them in effects or handlers.
Common mistakes
- Reading or writing the DOM during render.
- Using useEffect for measurements and causing a visible flicker.
- Expecting ref.current to be set during render.
- Putting subscriptions in the render body.
- Assuming effects run before the browser paints.
Follow-ups
- Why does React keep the commit phase synchronous?
- When can passive effects run before the paint?
- How does Strict Mode change effect timing in development?
Tips
- Describe the phases as "compute, then apply".
- Name the commit sub-steps in order with where refs and layout effects fit.
- Mention that render can be repeated, which is the reason for purity.