What is the Virtual DOM in React and how does it work?
- A.A built-in browser API that React calls to render faster
- B.A cache of already-rendered HTML strings so the next render can be skipped
- C.An in-memory UI tree; React diffs the new tree against the old one and then updates the real DOM
- D.A copy of the DOM running in a Web Worker, letting React render across several threads
Answer: C
Virtual DOM is an in-memory representation of the real DOM. When state changes, React creates a new Virtual DOM tree, diffs it against the old one to compute the minimal set of changes, then applies them to the real DOM (reconciliation). The goal is to reduce expensive DOM operations, not to always be faster than direct DOM manipulation.
References