Back to blog
ReactJS

How React Virtual DOM Works Under the Hood

May 14, 2026 4 min read

The Problem React Needed to Solve

Web pages are built using the DOM (Document Object Model).

The DOM represents HTML elements as objects.

Example:

<h1>Hello</h1>

becomes a DOM node inside the browser.


Why Direct DOM Updates Are Expensive

Updating the Real DOM frequently is slow because browsers must:

  • Recalculate layout

  • Repaint elements

  • Re-render UI parts

For small apps this is fine.

But large applications may update UI thousands of times.

Example:

  • Notifications

  • Chat apps

  • Dashboards

  • Realtime data

Frequent direct DOM manipulation becomes expensive.


Real DOM Update Problem

Imagine updating 1 item inside a list of 1000 elements.

Without optimization:

Browser may reprocess large parts of UI

This hurts performance.


What Is the Virtual DOM?

React solves this using the Virtual DOM.

The Virtual DOM is:

A lightweight JavaScript representation of the Real DOM

Instead of touching the browser DOM directly every time, React works with this lightweight copy first.


Real DOM vs Virtual DOM

Real DOMVirtual DOM
Actual browser DOMJavaScript representation
Expensive to updateCheap to create/update
Browser handles itReact handles it
Slower frequent updatesFaster comparison process

Simple Mental Model

Think of the Virtual DOM like a draft copy.

React first changes the draft version.

Then it compares:

Old draft vs New draft

and updates only what actually changed.


Initial Render Process

When a React component renders for the first time:

  1. React creates Virtual DOM tree

  2. React converts it into Real DOM

  3. Browser displays UI


Example Component

function App() {
 return Hello;
}

React internally creates a Virtual DOM representation of this UI.


Initial Render Flow

Diagram: flowchart LR

What Happens When State Changes?

React components re-render when:

  • State changes

  • Props change

Example:

const [count, setCount] = useState(0);

Calling:

setCount(1)

triggers a re-render.


Important Point

React does NOT immediately rebuild the whole browser UI.

Instead:

React creates a NEW Virtual DOM tree


State Update Lifecycle

Diagram: flowchart TD

Example

Initial UI:

<h1>0</h1>

After state update:

<h1>1</h1>

React now compares:

  • Old Virtual DOM

  • New Virtual DOM


What Is Diffing?

Diffing means:

Comparing old Virtual DOM tree with new Virtual DOM tree

React checks:

  • What changed?

  • What stayed same?

  • What should update?

This process is also called:

Reconciliation


Old Tree vs New Tree

Diagram: flowchart LR

How React Finds Minimal Changes

React intelligently identifies only changed nodes.

Example:

Before:

<h1>Hello</h1>

After:

<h1>Hello World</h1>

React notices:

Only text changed

So it updates only that node.


Why This Improves Performance

Instead of rebuilding everything:

React updates only necessary parts

This reduces expensive browser DOM operations.


Minimal Patch Update

Diagram: flowchart LR

Real DOM Is Updated Last

After diffing finishes:

  1. React prepares minimal updates

  2. React updates changed nodes

  3. Browser reflects changes

This phase is called:

Commit phase


Full React Lifecycle Flow

Diagram: flowchart TD

Example Walkthrough

Component:

function Counter() {
  const [count, setCount] = useState(0);

  return (
    <div>
      <h1>{count}</h1>

      <button
        onClick={() => setCount(count + 1)}
      >
        Increment
      </button>
    </div>
  );
}


Initial Render

Virtual DOM:

div
 ├── h1(0)
 └── button

React creates matching Real DOM.


After Button Click

New Virtual DOM:

div
 ├── h1(1)
 └── button

React compares old vs new trees.

It sees:

Only h1 text changed

Button stays untouched.


Important Optimization Idea

React avoids unnecessary Real DOM updates because:

Real DOM operations are expensive

while Virtual DOM operations are cheap JavaScript operations.


Common Beginner Misunderstanding

Incorrect:

React replaces the entire page every update

Correct:

React updates only changed parts of UI


Another Important Misunderstanding

Incorrect:

Virtual DOM is faster than DOM itself

Correct:

Efficient diffing reduces expensive DOM operations


Where This Helps Most

Virtual DOM is especially useful in:

  • Realtime dashboards

  • Social media feeds

  • Chat applications

  • Interactive forms

  • Large UI applications


High-Level Render → Diff → Commit Flow

PhaseWhat Happens
RenderCreate Virtual DOM
DiffCompare old vs new tree
CommitApply minimal Real DOM updates

Key Takeaways

ConceptSummary
Real DOMActual browser DOM
Virtual DOMLightweight JS representation
Re-renderCreates new Virtual DOM tree
DiffingCompare old vs new trees
ReconciliationFind minimal UI changes
CommitUpdate Real DOM

Final Notes

The Virtual DOM is one of React’s most important ideas because it allows React to build highly interactive UIs efficiently without manually managing DOM updates.

The important mental model is:

React compares UI changes first,
then updates only what actually changed


0 Comments

Sign in to join the conversation

No comments yet. Be the first to comment!