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:
React updates Virtual DOM first
Compares changes
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
countvalue 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 π





