Lesson 17 of 37Effects and beyond
How Rendering Works
Follow an update from trigger to render to commit, and see how React diffs trees, preserves state, and stays responsive.
Most confusing React behaviour (a component that re-renders "for no reason", state that survives when you expected it to reset, a list that loses its input text) makes sense once you know what React does between setState and the pixels changing. This is also the classic senior interview topic: "explain the virtual DOM and reconciliation".
Trigger, render, commit
Every screen update goes through three steps.
- Trigger. Something asks for an update: the first
root.render(<App />), or a state setter likesetCount. - Render. React calls your components to find out what the UI should look like. The result is a tree of plain objects (React elements), sometimes called the virtual DOM. Nothing on screen changes in this step.
- Commit. React compares the new tree with the previous one and applies the minimum set of changes to the real DOM. Then the browser paints, and then effects run.
"Rendering" in React means calling your function, not touching the DOM. A component can render and produce exactly the same output, in which case the commit changes nothing.
What re-renders when state changes
When a component's state changes, React re-renders that component and everything below it, by default. It does not re-render parents or siblings.
The first render calls every component once, top to bottom.
Watch it happen. Each component logs when it renders.
TodoList holds a filter state and the user changes it. Which components re-render?Reconciliation: how React diffs two trees
Comparing two arbitrary trees perfectly is very slow (roughly O(n³)). React uses two assumptions to make it O(n):
- Different element type, different tree. If a
<div>becomes a<section>, or<Counter>becomes<Timer>, React throws away the old subtree (unmounting components and losing their state) and builds a new one. - Keys identify children. In a list, React matches old and new children by
key. Same key, same component instance, state kept. New key, new instance.
If the type and key at a position stay the same, React keeps the instance and its state and only updates the props. State belongs to a position in the tree, not to the component function.
Click the counter a few times. Changing the label keeps the count (same type, same position, new props). Switching the wrapper from div to section resets it to 0 (different type above it, whole subtree rebuilt).
{isEditing ? <Form key="edit" /> : <Form key="new" />}. The user types into the form and then isEditing flips. What happens to the typed text?What does React do with the old instance?
Batching
Calling three setters in one event handler does not render three times. React batches all updates queued during an event and renders once afterwards. Since React 18 this also happens inside setTimeout, promises, and native event listeners (automatic batching).
The component has rendered once. What is the next line logged after one click?
function App() {
const [a, setA] = useState(0);
const [b, setB] = useState(0);
console.log("render", a, b);
function handleClick() {
setA(1);
setB(2);
setA(a + 5);
}
return <button onClick={handleClick}>Go</button>;
}Fiber: rendering in small pieces
Before React 16, rendering was one recursive call that could not stop. A large update could freeze the page for hundreds of milliseconds while the user's typing waited. React rewrote its core as Fiber.
A fiber is a small object per component instance that records its type, props, state (the hook list from the custom hooks lesson lives here), and links to its parent, child, and sibling. Because the work is a linked structure instead of a call stack, React can process a few fibers, check whether something more urgent arrived (like a keystroke), pause, handle it, and resume or throw away the half-finished render.
Under the hood: two trees
React keeps two fiber trees: the current tree, matching what is on screen, and a work-in-progress tree it builds during render. Render work can be paused or discarded because it happens on the work-in-progress copy. When it is complete, the commit phase applies the DOM changes in one go (commit is never interrupted, so the user never sees a half-updated screen) and the work-in-progress tree becomes the new current tree.
Concurrent features: marking work as non-urgent
Interruptible rendering is what powers React's concurrent features. You tell React which updates are urgent (typing, clicking) and which can wait (filtering a big list).
useTransitiongives youstartTransition(fn). State updates insidefnare low priority and can be interrupted.isPendingistruewhile the transition renders. In React 19,fncan also beasync(an Action).useDeferredValue(value)gives you a copy ofvaluethat lags behind during heavy renders, so the urgent part (the input) stays snappy.
Type quickly in the box below. Each item is artificially slow. With the deferred value, the input keeps up while the list catches up behind it.
const [isPending, startTransition] = useTransition();
function handleTabClick(next: Tab) {
startTransition(() => {
setTab(next); // React may interrupt this render if the user clicks again
});
}startTransition?Reset state with a key
In this chat app the draft message leaks between contacts: type hi to Ada, switch to Sam, and the draft still says hi. React sees the same <Chat> at the same position, so it keeps its state.
Make the draft reset when you switch to a different contact, without moving the draft state up or adding effects. Clicking the contact you are already on must keep the draft.
- The label reads
Message to <name>. - Buttons: Ada, Sam, Priya.
- render phase
- React calls your components to compute the next UI tree. Pure and interruptible; nothing on screen changes.
- commit phase
- React applies the minimal DOM changes from the diff, then the browser paints and effects run.
- reconciliation
- Diffing the new element tree against the previous one, matching children by type and key.
- Fiber
- React's internal unit of work, one per component instance, which lets rendering pause, resume, and be discarded.
- automatic batching
- React groups several state updates into one render, including inside timeouts and promises.
- transition
- A low-priority update started with
startTransition, which React can interrupt to keep the UI responsive.