Skip to main content

Command Palette

Search for a command to run...

How React Virtual DOM Works Under the Hood

Updated
β€’8 min read

Agar aap React seekh rahe ho, toh ek term aapne bahut baar suni hogi:

"React is fast because of the Virtual DOM."

Lekin actual question ye hai:

πŸ‘‰ Virtual DOM hota kya hai?

πŸ‘‰ React isko use kaise karta hai?

πŸ‘‰ State change hone par exactly kya hota hai?

Is article me hum Virtual DOM ko beginner-friendly Hinglish me samjhenge aur React ke complete render β†’ diff β†’ update flow ko step by step dekhenge.


The Problem: Why Direct DOM Manipulation Is Slow?

Sabse pehle problem samajhte hain.

Browser web page ko DOM (Document Object Model) ke form me represent karta hai.

Example:

<div>
  <h1>Hello</h1>
  <button>Click Me</button>
</div>

Browser internally kuch aisa tree banata hai:

div
β”œβ”€β”€ h1
└── button

Ab maan lo page par hundreds ya thousands of elements hain.

Agar har state change par directly DOM update karna pade:

  • Browser ko elements find karne padenge

  • Layout recalculate karna padega

  • Repaint karna padega

  • Re-render karna padega

Ye operations expensive hote hain.

Isi problem ko solve karne ke liye React Virtual DOM use karta hai.


What Is the Real DOM?

Real DOM wahi actual DOM hai jo browser maintain karta hai.

Characteristics:

  • Browser ke andar exist karta hai

  • Visible UI represent karta hai

  • Direct updates expensive ho sakte hain

  • Frequent changes performance ko affect kar sakte hain

Example:

Browser DOM

App
 β”œβ”€β”€ Navbar
 β”œβ”€β”€ Sidebar
 └── Content

Ye hi actual structure screen par render hota hai.


What Is the Virtual DOM?

Virtual DOM ek lightweight JavaScript representation hai Real DOM ki.

Simple words me:

Real DOM ka virtual copy.

Example:

{
  type: "h1",
  props: {
    children: "Hello React"
  }
}

Ye browser DOM nahi hai.

Ye sirf JavaScript object hai jo memory me store hota hai.

Benefits:

  • Create karna fast

  • Compare karna fast

  • Manipulate karna easy


Real DOM vs Virtual DOM

Real DOM Virtual DOM
Actual browser DOM JavaScript copy
Slow updates Fast updates
Direct manipulation expensive Comparison inexpensive
Browser handles it React handles it
Causes layout/repaint No browser repaint

Simple rule:

React pehle Virtual DOM ko update karta hai, phir minimum changes Real DOM me apply karta hai.


Initial Render Process in React

Jab React app first time load hoti hai, ye steps follow hote hain.

Step 1: Component Render

function App() {
  return <h1>Hello React</h1>;
}

React component execute karta hai.


Step 2: Virtual DOM Tree Create Hoti Hai

React ek Virtual DOM object create karta hai.

Virtual DOM

App
 └── h1
      └── Hello React

Step 3: Real DOM Create Hota Hai

Ab React Virtual DOM ke basis par actual DOM create karta hai.

Real DOM

App
 └── h1
      └── Hello React

Initial Render Flow

React Component
        β”‚
        β–Ό
   Virtual DOM
        β”‚
        β–Ό
    Real DOM
        β”‚
        β–Ό
      Screen

First render me poora UI create hota hai.


What Happens When State Changes?

Example:

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

User button click karta hai:

setCount(count + 1);

Ab React ko UI update karna padega.

Lekin poora page update nahi kiya jata.

Yahi Virtual DOM ka magic hai.


State Change Triggers Re-render

State update hone par:

setCount(1);

React component ko dobara run karta hai.

function App() {
  return <h1>Count: 1</h1>;
}

Is process ko re-render kehte hain.

Important:

React poori screen redraw nahi karta.

Sirf component function dobara execute karta hai.


Creation of a New Virtual DOM Tree

Re-render ke baad React ek naya Virtual DOM tree create karta hai.

Old Tree:

App
 └── h1
      └── Count: 0

New Tree:

App
 └── h1
      └── Count: 1

Ab React ke paas do trees hain:

  • Old Virtual DOM

  • New Virtual DOM

Next step hai comparison.


What Is Diffing (Reconciliation)?

React old tree aur new tree ko compare karta hai.

Is comparison process ko:

Diffing ya Reconciliation kehte hain.

Goal:

πŸ‘‰ Find karo ki exactly kya change hua hai.

Example:

Old:

<h1>Count: 0</h1>

New:

<h1>Count: 1</h1>

React detect karta hai:

βœ… h1 same hai

βœ… Structure same hai

βœ… Sirf text change hua hai

Bas itna hi update karna hai.


Old Tree vs New Tree Comparison

OLD TREE

App
 └── h1
      └── Count: 0


NEW TREE

App
 └── h1
      └── Count: 1

Diff Result:

Only Text Changed

No need to recreate:

  • App

  • h1

Sirf text node update hoga.


How React Finds Minimal Required Changes?

React har update me ye questions poochta hai:

Kya Element Type Same Hai?

<h1>Hello</h1>

↓

<h1>Welcome</h1>

Type same hai.

Sirf content update hoga.


Kya Props Change Hui Hain?

<button disabled={false}>

↓

<button disabled={true}>

React sirf disabled prop update karega.


Kya Child Nodes Change Hue Hain?

<ul>
  <li>A</li>
</ul>

↓

<ul>
  <li>A</li>
  <li>B</li>
</ul>

React sirf new item add karega.

Poora list recreate nahi karega.


Updating Only Changed Nodes in the Real DOM

Diffing ke baad React ek update plan banata hai.

Example:

Old:

<h1>Count: 0</h1>

New:

<h1>Count: 1</h1>

React directly ye nahi karta:

❌ Remove h1

❌ Create new h1

Instead:

βœ… Update text content

Result:

Fast update.

Less browser work.

Better performance.


Minimal Update Patch

Old DOM
    β”‚
    β–Ό
Find Difference
    β”‚
    β–Ό
Create Patch
    β”‚
    β–Ό
Update Only Changed Part

Yehi Virtual DOM ka main purpose hai.


Why This Approach Improves Performance?

Imagine:

Page me 1000 elements hain.

User ek counter increment karta hai.

Without optimization:

Update 1000 Elements

With Virtual DOM:

Update Only 1 Element

Browser workload dramatically reduce ho jata hai.

Benefits:

  • Faster UI updates

  • Less DOM manipulation

  • Better responsiveness

  • Smoother user experience


React Render β†’ Diff β†’ Commit Flow

React ka high-level lifecycle kuch aisa hota hai:

1. Render Phase

Component execute hota hai.

Component Render

2. New Virtual DOM Created

React naya tree create karta hai.

New Virtual DOM

3. Diffing (Reconciliation)

Old aur new tree compare hote hain.

Old Tree
   VS
New Tree

4. Changes Identified

React decide karta hai:

What Changed?

5. Commit Phase

Minimum updates Real DOM me apply hoti hain.

Update Real DOM

6. Browser Updates Screen

Final UI user ko dikhti hai.

Screen Updated

Complete React Update Lifecycle

State / Props Change
          β”‚
          β–Ό
     Re-render
          β”‚
          β–Ό
 New Virtual DOM
          β”‚
          β–Ό
 Compare with Old Tree
          β”‚
          β–Ό
      Diffing
          β”‚
          β–Ό
  Find Differences
          β”‚
          β–Ό
   Create Updates
          β”‚
          β–Ό
    Update Real DOM
          β”‚
          β–Ό
   Updated UI Shown

Mental Model to Remember

Agar Virtual DOM ko ek sentence me samajhna ho:

React directly browser DOM ke saath fight nahi karta. Pehle ek lightweight Virtual DOM create karta hai, old aur new versions compare karta hai, aur phir sirf required changes Real DOM me apply karta hai.

Isi wajah se React applications responsive aur efficient feel karti hain.


Final Thoughts

Virtual DOM React ka core performance optimization concept hai.

Har update par React:

  • Component re-render karta hai

  • Naya Virtual DOM create karta hai

  • Old aur new tree compare karta hai

  • Differences find karta hai

  • Sirf changed nodes update karta hai

Is process ko hi Reconciliation kaha jata hai.

Aapko React ke internals ya Fiber architecture jaane ki zaroorat nahi hai Virtual DOM samajhne ke liye.

Bas ye mental model yaad rakho:

Render β†’ New Virtual DOM β†’ Diff β†’ Minimal Updates β†’ Real DOM Update

Aur yahi React ko modern frontend development me itna powerful banata hai. πŸš€