wasAsked.

What is the virtual DOM in React and how does reconciliation work?

Frequently Asked (All Companies) · Role not specified · technical

Published

Year not specified · React, JavaScript

Community answers

Posted by wasAsked editorial collection

The virtual DOM is an in-memory representation of the UI as lightweight JavaScript objects. React uses it to decide the minimum set of changes needed to update the real browser DOM, because direct DOM manipulation is comparatively expensive. How it works 1. Render: when state or props change, React calls your component functions to produce a new tree of React elements (the virtual DOM description of what the UI should look like). 2. Reconciliation (diffing): React compares the new tree with the previous one to find what changed. 3. Commit: React applies only the necessary changes to the real DOM (inserting, updating or removing nodes and attributes), then runs effects. const [count, setCount] = useState(0); return <button onClick={() => setCount(count + 1)}>Clicked {count} times</button>; When count changes, React re-runs the component, sees that only the text inside the button changed, and updates just that text node. The reconciliation algorithm Comparing two arbitrary trees optimally is very expensive (roughly O(n³)), so React uses heuristics that make diffing O(n): 1. Different element types produce different trees. If a <div> becomes a <section>, or ComponentA becomes ComponentB, React destroys the old subtree (including component state) and builds a new one. 2. Same element type: React keeps the DOM node and updates only changed attributes and props, then recurses into children. 3. Keys identify list items. When rendering lists, React uses the key prop to match children between renders, so reordering, insertion and deletion are handled efficiently and component state stays with the right item. Fiber architecture Since React 16, reconciliation is implemented with "Fiber", which breaks rendering work into units that can be paused, prioritised and resumed. This enables concurrent features in React 18 and 19: automatic batching of state updates, transitions (useTransition) to keep the UI responsive during heavy updates, Suspense for data and code loading, and streaming server rendering. Common misconceptions - "The virtual DOM makes React faster than the DOM." Not exactly: the virtual DOM adds work. Its value is making declarative UI updates efficient enough, so developers describe what the UI should look like instead of writing manual DOM updates. - "React re-renders the whole page." React re-renders components whose state or props changed (and their children by default), but only commits actual differences to the DOM. Performance tips related to reconciliation - Use stable, unique keys for list items (not array indexes for lists that change order). - Avoid creating component definitions inside other components, which changes the element type every render and remounts the subtree. - Use React.memo, useMemo and useCallback (or the React Compiler) to skip re-rendering unchanged subtrees when profiling shows a need. - Keep state as local as possible so fewer components re-render. Other frameworks Some newer frameworks (Svelte, SolidJS, and Angular with signals) update the DOM without a virtual DOM by tracking fine-grained dependencies at compile time or runtime — a useful comparison point in interviews. Interview summary: "The virtual DOM is React's in-memory UI tree. On each update React renders a new tree, diffs it against the previous one using type and key heuristics (reconciliation), and commits only the minimal DOM changes. Fiber makes this work interruptible to support concurrent features."

Ravindra Wadile · · 0 likes

Sign in to add your answer

Related interview topics