ReactEasy
What is the Virtual DOM?
The Virtual DOM is the in-memory tree of React elements that describes the UI. When state changes, React renders a new tree, compares it with the previous one, and applies only the differences to the real DOM, which is slower to manipulate directly.
How it fits together
- Components return JSX, which becomes React elements (plain objects).
- React keeps a working tree of these (internally called fibers) plus the current one on screen.
- On an update, React re-renders the affected components into a new tree.
- Reconciliation diffs old and new trees.
- Commit applies the minimal set of DOM operations (insert, update attributes, remove).
What it is good for
- Declarative code: you re-describe the whole UI; React computes the changes.
- Batching: many state updates can become one DOM update.
- Scheduling: concurrent rendering can pause, prioritize or discard work before committing.
- Portability: the same model drives React Native, React Three Fiber and other renderers.
Common misconception
The Virtual DOM is not magic speed. Diffing has a cost; hand-written DOM updates can be faster. Its value is making UI updates predictable and fast enough by default. Other frameworks (Svelte, Solid) use compile-time or fine-grained reactivity instead.
Related modern pieces
- React Compiler reduces how much of the tree re-renders.
- Server Components render on the server and send a serialized tree (the RSC payload), not a Virtual DOM diff.
Code example
tsx
1// Each render produces a new element tree; React diffs it2import { useState } from "react";34export function Counter() {5 const [count, setCount] = useState(0);67 // Render 1 tree: <div><h1>0</h1><button>+1</button></div>8 // Render 2 tree: <div><h1>1</h1><button>+1</button></div>9 // Diff result: only the <h1> text node changes in the real DOM10 return (11 <div>12 <h1>{count}</h1>13 <button onClick={() => setCount((c) => c + 1)}>+1</button>14 </div>15 );16}1718// Roughly, the "virtual" description is just objects:19const tree = {20 type: "div",21 props: { children: [{ type: "h1", props: { children: 0 } }, { type: "button", props: { children: "+1" } }] },22};23export { tree };
Practice this question
Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the React set.