The virtual DOM and reconciliation
What the virtual DOM actually is, how React diffs trees to update the real DOM efficiently, and where Fiber fits in.
The virtual DOM and reconciliation
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- The virtual DOM is a plain JavaScript object tree — built from the React elements your JSX compiles to — that describes what the UI should look like, kept separate from the actual, much slower DOM.
- Reconciliation is the process of comparing a new virtual DOM tree against the previous one to work out the minimal set of real DOM changes needed, instead of rebuilding the page from scratch.
- Diffing uses heuristics, not an exhaustive comparison: same element type at the same position gets updated in place, a different type gets torn down and replaced, and lists rely on keys to track identity across renders.
- This matters because real DOM operations are comparatively expensive; computing a diff over lightweight JavaScript objects first, then applying only the necessary changes, is far cheaper than mutating the DOM directly for every piece of state that changes.
- Fiber is the name of React's current reconciliation engine, which reworked this process into interruptible units of work so rendering can pause, prioritize urgent updates, and resume — a detail worth knowing exists without needing its internals yet.
A description, not a picture
Every time a component renders, its JSX compiles down to calls that build a tree of plain JavaScript objects — one object per element, each describing a type, its props, and its children. React calls this tree the virtual DOM. Despite the name, it isn’t a DOM at all; it’s ordinary data, cheap to create and cheap to throw away, that happens to describe what a DOM would look like if you built it from that description.
This separation is the whole point. The real DOM, the thing the browser actually renders, is comparatively expensive to work with: changing it can trigger layout recalculation and repainting, and doing that for every small state change would make interactive UIs feel sluggish fast. A JavaScript object, by contrast, costs almost nothing to create or compare. By building a lightweight description first and figuring out what actually needs to change before touching the real DOM, React keeps the expensive part — real DOM mutation — as small as it can.
Reconciliation: finding the difference
Every time a component’s state changes, React doesn’t start from a blank page. It re-runs the relevant render functions, producing a new virtual DOM tree, and then compares that new tree against the one from the previous render. This comparison process is called reconciliation, and the output is a minimal set of real DOM operations — “update this attribute,” “insert this node,” “remove that one” — needed to bring the real DOM in line with the new description.
A full, generic tree-diffing algorithm is expensive to compute in the mathematically general case. React avoids that cost with a few practical assumptions that hold for almost all real UIs:
- Same type at the same position → update in place. If a
<div>renders where a<div>rendered before, React assumes it’s “the same” conceptual element, keeps the underlying DOM node, and just updates whatever props changed. - Different type at the same position → replace. A
<div>where a<span>used to be is treated as a structurally different thing; React discards the old subtree and builds a fresh one, rather than trying to patch incompatible nodes. - Lists need an explicit identity. When comparing two lists of children, React can’t always tell which old item corresponds to which new item just by position — items might have been reordered, inserted, or removed. That’s what the
keyprop solves, covered in its own lesson shortly.
The practical upshot: a counter going from 3 to 4 doesn’t trigger React to rebuild the whole page’s DOM. It triggers a render that produces a new virtual tree differing from the old one in exactly one spot — a text node — and reconciliation narrows the real DOM work down to touching just that spot.
Why this avoids full-tree rewrites
Compare this to the imperative alternative, hinted at in the first lesson of this roadmap: if you were manually managing the DOM, you would need to track, by hand, exactly which operations each state change requires, and write that logic explicitly everywhere state changes. React’s bet is that it’s more sustainable to let you describe the whole UI freshly on every render — which is dramatically simpler to write and reason about — and let a generic algorithm work out the minimal real update, than to ask every developer to hand-optimize DOM updates themselves. Reconciliation is what makes “just describe the whole thing again” not a performance disaster.
Fiber, honestly
The reconciliation approach described above has existed since React’s early versions, but its implementation was substantially reworked starting with React 16 into what’s called Fiber. The core idea worth taking away, without going deep into its internals: older React performed a reconciliation pass as one long, uninterruptible stretch of synchronous work, which could make an app feel unresponsive during a big update. Fiber restructured that work into small units that can be paused, resumed, prioritized, or abandoned — so React can, for example, interrupt a big, low-priority re-render to handle a user’s keystroke immediately, then pick the bigger render back up. This capability underlies features covered much later in this roadmap, like transitions and concurrent rendering. For now, it’s enough to know Fiber is the name of the engine doing reconciliation, and that “interruptible, prioritized units of work” is the one-sentence version of why it exists — the mechanism’s finer details aren’t needed to write React components productively.
What the video covers
Tejas Kumar’s talk covers exactly this lesson’s scope: what the virtual DOM is, how the diffing algorithm works conceptually, why that avoids full-DOM rewrites, and why Fiber replaced React’s older reconciler. It’s presented at a conceptual level rather than as a line-by-line tour of React’s source, which matches the depth this lesson aims for — don’t expect (or need) implementation-level detail on Fiber from it or from this lesson; that’s a specialized topic well beyond what using React day to day requires.
Where this fits
With “what React does with your JSX” covered, the next lesson goes back to the building blocks you’ll actually write: components and props.
Check yourself
What is the virtual DOM, concretely?
A tree of plain JavaScript objects, produced from your components' JSX, that describes what the UI should look like at a given moment. It isn't rendered itself — it's a lightweight description that React compares against the previous one to figure out what the real DOM needs.
Why doesn't React just re-render the entire real DOM from scratch every time state changes?
Real DOM operations are comparatively slow — they trigger layout and paint work in the browser. Diffing two lightweight JavaScript object trees first, then touching only the real DOM nodes that actually changed, is far cheaper than discarding and rebuilding the whole page for every update.
At a high level, what does React do differently when it sees two sibling elements of a different type at the same position in a tree, versus the same type?
When the type matches (say, a <div> replaced by another <div>), React updates that existing node's attributes and recurses into its children rather than discarding it. When the type differs (a <div> replaced by a <span>, for instance), React tears down the old subtree and builds a new one in its place, rather than trying to reconcile incompatible nodes.
What is Fiber, at the level this lesson expects you to know it?
Fiber is the name of React's reconciliation engine since React 16, which restructured the diffing work into small, interruptible units so React can pause lower-priority rendering work, handle something more urgent, and resume — rather than running the whole diff as one uninterruptible block. The deep internals of Fiber are not needed to use React productively.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.