Skip to main content

Command Palette

Search for a command to run...

How React Virtual DOM Works Under the Hood

Updated
β€’4 min read
How React Virtual DOM Works Under the Hood

🧠 Introduction

Modern web apps need to be fast, smooth, and responsive.
But updating the UI directly using the browser DOM can be slow and inefficient.

πŸ‘‰ This is where React Virtual DOM comes in.

In this blog, we’ll understand:

  • What problem Virtual DOM solves

  • How React updates UI step by step

  • What diffing (reconciliation) means

  • Why React is fast

❌ Problem: Slow Real DOM Updates

The Real DOM (browser DOM) is:

  • Heavy ❌

  • Slow to update ❌

  • Expensive for frequent changes ❌

Example:

If you update 1000 elements:

  • Browser may re-render everything 😡

  • Causes lag & poor performance

⚑ Solution: Virtual DOM

πŸ‘‰ Virtual DOM = Lightweight JavaScript copy of Real DOM

Instead of updating Real DOM directly:

  1. React updates Virtual DOM first

  2. Compares changes

  3. Updates only required parts

βš–οΈ Real DOM vs Virtual DOM

Feature Real DOM Virtual DOM
Speed Slow Fast
Updates Direct Optimized
Cost High Low
Control Browser React

πŸ”„ Step-by-Step: How React Updates UI

/

🟒 1. Initial Render

When app loads:

Component β†’ Virtual DOM β†’ Real DOM

Flow:

  • React creates Virtual DOM tree

  • Converts it into Real DOM

  • UI appears on screen

πŸ’» Real Example (Counter App)

Let’s understand this using a simple React app:

πŸ“¦ React DOM Entry Point

This is how React connects to browser:

main.jsx

import { StrictMode } from 'react'
import { createRoot } from 'react-dom/client'
import './index.css'
import App from './App.jsx'

createRoot(document.getElementById('root')).render(
  <StrictMode>
    <App />
  </StrictMode>,
)

🌐 HTML Root File

index.html

<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>React App</title>
  </head>
  <body>
    <div id="root"></div>
    <script type="module" src="/src/main.jsx"></script>
  </body>
</html>

App.jsx

import { useState } from "react";

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

  return (
    <>
      <h1>Counter App!</h1>
      <h3>Counter: {count}</h3>

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

      <button onClick={() => setCount(count - 1)}>
        Decrement
      </button>
    </>
  );
}

export default App;

πŸ” Initial Render Flow

App Component
   ↓
Virtual DOM Tree Created
   ↓
Converted to Real DOM
   ↓
Displayed on Screen

Virtual DOM

πŸ”΅ 2. State Change Trigger

When user clicks button:

setCount(count + 1);

πŸ‘‰ React triggers re-render


🟑 3. New Virtual DOM Tree Created

After state change:

  • React creates a new Virtual DOM

  • Updates count value inside it

🟠 4. Diffing (Reconciliation)

React compares:

Old Virtual DOM vs New Virtual DOM

πŸ‘‰ It finds that only this changed:

Counter: 0 β†’ Counter: 1

🟣 5. Minimal Update Found

React identifies:

  • Only text node changed

  • Buttons & structure remain same


πŸ”΄ 6. Real DOM Update

πŸ‘‰ React updates ONLY:

<h3>Counter: 0</h3>
        ↓
<h3>Counter: 1</h3>

βœ… No full re-render
βœ… Only minimal update

πŸ”„ Full Lifecycle Flow

1. Initial Render Component β†’ Virtual DOM β†’ Real DOM 
2. State Change (Button Click)
          ↓ 
3. New Virtual DOM Created 
          ↓ 
4. Diffing (Old vs New)
          ↓ 
5. Minimal Changes Found 
          ↓
6. Real DOM Updated

⚑ Why Virtual DOM is Fast?

βœ… Because:

  • Avoids direct DOM manipulation

  • Updates only changed parts

  • Reduces unnecessary re-renders


🧠 Key Concept

πŸ‘‰ React is NOT faster because it replaces DOM
πŸ‘‰ It is faster because it does LESS work


πŸ’‘ Real-Life Analogy

Imagine editing a book:

❌ Real DOM β†’ Rewrite full page
βœ… Virtual DOM β†’ Update only one line


πŸš€ Final Thoughts

React Virtual DOM works like a smart middle layer:

  • Tracks changes

  • Compares efficiently

  • Updates only what’s needed

πŸ‘‰ That’s why React apps feel fast and smooth


🧾 Summary

  • Real DOM updates are expensive

  • Virtual DOM is a lightweight copy

  • State change creates new Virtual DOM

  • Diffing compares trees

  • Only minimal updates applied

  • Improves performance significantly


πŸ“Œ Bonus Tip

πŸ‘‰ Focus on understanding flow, not internals
πŸ‘‰ Virtual DOM = Efficiency, not magic


πŸ™Œ Acknowledgement

Learning as part of Mobile Development Cohort πŸš€

More from this blog

M

Mobile Development

5 posts

Mobile Development is a complete, step-by-step learning series focused on building modern mobile applications using React, React Native, and Expo.

This series covers everything from React fundamentals (Virtual DOM, hooks, component-based architecture) to advanced mobile development concepts like navigation, API integration, authentication, device features, and production deployment.