How React Virtual DOM Works Under the Hood
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
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:
React creates Virtual DOM tree
React converts it into Real DOM
Browser displays UI
Example Component
function App() {
return Hello;
}React internally creates a Virtual DOM representation of this UI.
Initial Render Flow
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
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
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
Real DOM Is Updated Last
After diffing finishes:
React prepares minimal updates
React updates changed nodes
Browser reflects changes
This phase is called:
Commit phase
Full React Lifecycle Flow
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
Key Takeaways
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!