React / 27
Virtual DOM, reconciliation & Fiber
On every update React builds a new tree of elements, compares it with the last one, and changes only the DOM that differs. Here's what it compares, and the myths to drop.
Render, then commit
Trigger
State changes (or the app starts).Render
React calls your components. They return JSX, which becomes a tree of plain objects: React elements, as you saw in Chapter 1.Reconcile
React compares the new tree with the previous one to find what changed. This comparing is called reconciliation, or diffing.Commit
React applies only those changes to the real DOM, in one go. Then the browser paints.
That tree of plain objects is what people call the virtual DOM: a cheap, in-memory description of the UI. React's own docs mostly say "render" and "commit" these days, but the idea is the same.
previous render
this render
commit · the only DOM write
p.textContent = "3 items"What React compares
Finding the perfect minimum set of changes between two trees takes roughly n³ steps for n elements, far too slow for a UI. React uses a fast O(n) shortcut built on a few rules:
- Different type at the same spot: a
divbecame asection, or<Login />became<Dashboard />. React assumes the whole subtree is different. It destroys the old one, state included, and builds the new one from scratch. - Same type at the same spot: React keeps the DOM node or component instance, state included, and only updates the props or attributes that changed. Then it repeats the comparison for the children.
- Lists of children: matched by key. Without keys, by position. That's why the keys topic matters.
What React compares
Try it<div className="card"><LikeCounter /></div>0 likes
// Same position, different type → Form's state is thrown away on each toggle
{isCompact ? <div><Form /></div> : <section><Form /></section>}
// Same type, only a prop changes → Form keeps its state
<div className={isCompact ? "compact" : "roomy"}><Form /></div>Re-render is not repaint
"Component re-rendered" means React called your function again. If the result matches the last one, React touches no DOM at all. Calling a function and comparing objects is cheap; it's DOM changes and layout that are expensive, and those are what React keeps small.
When a component's state changes, React re-renders that component and its children, not the whole app.
Myths to drop
Don't
Myths
- "The virtual DOM is faster than the real DOM."
- "React re-renders the whole page on every change."
- "Fiber is what makes React fast."
Do
What's true
- Diffing is extra work. Hand-written DOM code can be faster; the virtual DOM makes declarative code fast enough without you touching the DOM.
- React re-renders the changed component and its children, then commits only real differences.
- Fiber is about scheduling: pausing and prioritising work so the UI stays responsive.
Some libraries (like Svelte and Solid) skip a virtual DOM entirely and update the DOM directly from compiled code. It's one good design among several, not magic.
React Fiber, in one paragraph
Fiber is the name of React's reconciler, the engine that does the render phase, rewritten for React 16 in 2017. Every component instance in your tree gets a fiber: a plain object holding its type, props, state (your hook slots live here) and links to its parent, child and sibling. Because the work is split into one small unit per fiber, React can pause rendering to handle something urgent like typing, then resume, or throw away work that's no longer needed. Features like startTransition (React 18+) are built on this. The commit phase is never split, so you never see a half-updated screen.
Gotchas
Recall
Can you answer these?
0/5
Answer out loud (or in your head) before you tap. That's what makes it stick.
Saved on this device only. No login.
