BugCast
React

Virtual DOM & Reconciliation

How React updates the UI efficiently — diffing algorithm and keys explained.

BugCast Admin··7 min read

Virtual DOM

A lightweight JavaScript representation of the real DOM. React compares old and new virtual trees to compute minimal DOM updates.

Reconciliation Process

  1. Render — create new virtual tree from state/props
  2. Diff — compare with previous tree
  3. Commit — apply changes to real DOM

Keys Matter

// Bad — index as key when list reorders
{items.map((item, i) => <li key={i}>{item.name}</li>)}

// Good — stable unique id
{items.map(item => <li key={item.id}>{item.name}</li>)}

Using index as key causes wrong component state when items are inserted/deleted/reordered.

Interview Questions

Q: Is Virtual DOM always faster than direct DOM?

No. For simple updates, direct DOM manipulation can be faster. Virtual DOM wins with complex UIs through batching and minimal updates.

Q: What is React Fiber?

React's reconciliation engine rewrite. Enables incremental rendering, prioritization, and Suspense.

Q: What triggers a re-render?

  • State change in component
  • Parent re-render (unless memoized)
  • Context value change for consumers
#virtual-dom#reconciliation#interview

Related posts