Course contentsShow
← React
Lesson 408 of 2,39612. Rendering and BatchingFree lesson

The Virtual DOM (Reconciliation)

React compares the new element tree with the previous one to determine minimal DOM changes needed.

The Virtual DOM (Reconciliation)

You'll learn how React efficiently updates the DOM by comparing versions of your UI.

What is the Virtual DOM?

When you call setState and trigger a render, React doesn't immediately change the actual browser DOM. Instead, it creates a lightweight JavaScript representation of what your UI should look like—this is the Virtual DOM. Think of it as a blueprint or draft copy of your UI tree.

React keeps two versions of this blueprint: the current one (representing what's on screen) and the new one (from your latest render). It then performs a comparison (called "diffing") between them to figure out exactly what changed.

The Reconciliation Process

Reconciliation is the algorithm React uses to diff the old and new Virtual DOM trees. Instead of throwing away the entire DOM and rebuilding it from scratch (which would be slow), React identifies the minimal set of changes needed.

For example:

  • If you toggle a single <div> from hidden to visible, React only updates that one element's attributes
  • If you change text in a button, React only updates that text node
  • If a component's state changes but the output is identical, React may skip DOM updates entirely

This is why your app feels fast even though every state change triggers a "re-render"—most of the time, React is just comparing JavaScript objects, which is cheap. Only the actual differences get applied to the real DOM.

Why This Matters

Without the Virtual DOM, you'd either manually track every tiny DOM change yourself (tedious and error-prone) or re-render everything (slow). React's reconciliation gives you the best of both worlds: declarative code that describes the full UI, with imperative performance under the hood.

Key Takeaway: React compares the new Virtual DOM with the previous one to calculate the minimal set of real DOM updates, making your declarative code performant without manual optimization.