LearnThatStack Ace your next interview
React · question
Question 1 of 103

What is the Virtual DOM and how does it work?

beginner
← All React questions
Re-explain

The Virtual DOM is an in-memory JavaScript representation of the Document Object Model (DOM), which React syncs with the real DOM.

When state changes, React takes four steps:

  1. React builds a new virtual DOM tree.
  2. React compares, or diffs, it with the previous tree.
  3. React works out the changes the real DOM needs.
  4. React applies only those changes to the real DOM.

This process is called reconciliation. The benefit is declarative code. You describe the UI, and React makes only the DOM writes it needs.

Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

The diagram below the answer is the concept . Jump to it ↓

Tailored explanation · switch back to · ·
What should the new diagram focus on?
Guided practice for React One question at a time. Answer out loud, get graded, see what you missed.
How well did you know this?
AI:

Saved in this browser - sign in to keep your review list.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Interview lens How interviewers actually use this question

Likely follow-ups

  • Is the Virtual DOM actually faster than updating the DOM by hand? Not by itself. Diffing adds work over a targeted manual write. The gain is fewer needless DOM writes while you keep declarative code.
  • Why do list items need a stable key during reconciliation? Keys let React match children across renders. Index keys break on insert or reorder, so React reuses the wrong state and does extra DOM work.
  • If a component re-renders, does the real DOM always change? No. A re-render builds a new virtual tree. If the diff finds nothing, React skips DOM writes, but the render and diff work still ran.
  • How does React decide whether to update a node or replace it during diffing? A different element type replaces that subtree. The same type keeps the node and updates changed props. Keys match children in lists.
  • What is React Fiber, and how does it change the way reconciliation runs? Fiber splits render work into units React can pause, prioritize and resume. The commit to the real DOM still happens in one go.

What you can say

  1. The Virtual DOM is an in-memory JavaScript copy of the DOM, and React keeps it in sync with the real DOM.
  2. When state changes, React first builds a new virtual tree for the UI.
  3. It diffs that new tree against the previous one and works out which changes the real DOM needs.
  4. Then it applies only those changes to the real DOM, and React calls this whole process reconciliation.
  5. The payoff is declarative code. I describe what the UI should look like, and React makes only the DOM writes it needs.
  6. It isn't free, though. React still renders and diffs on every update, so a careful manual DOM write can beat it, and big trees may need React.memo or good keys.

Weak answers to avoid

  • Says the Virtual DOM is faster than the real DOM Diffing is extra work, so a targeted manual update can beat it. Say it avoids needless DOM writes while you keep declarative code.
  • Confuses the Virtual DOM with the shadow DOM Shadow DOM is a browser feature that isolates a component's markup and styles. The Virtual DOM is React's in-memory tree. Mixing them up sounds memorized.
  • Defines it but never walks the diff steps The interviewer wants the mechanism. Walk it in order: build a new tree, diff it with the old one, work out the changes, apply only those to the real DOM.
Interview lens

Likely follow-ups, what you can say, and the weak answers to avoid.

Sign in free to open it Free account - the lens opens as soon as you're back.

Want a quick review of the fundamentals? See the React cheatsheet.

← Back to all React questions
Pro · $10/mo

88 of 103 React answers are in Pro.

Full answers, code samples, and AI explanations that go simpler or deeper. Cancel anytime.

  • Full answers + code
  • AI explanations, simpler or deeper
  • 1,000 AI credits / month
  • Cancel anytime