<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[native]]></title><description><![CDATA[native]]></description><link>https://expodev.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Sun, 01 Nov 2026 21:07:02 GMT</lastBuildDate><atom:link href="https://expodev.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[How React Virtual DOM Works Under the Hood]]></title><description><![CDATA[React’s Virtual DOM is one of the most talked-about concepts in frontend development. People often hear: “React is fast because of the Virtual DOM.” But…]]></description><link>https://expodev.hashnode.dev/how-react-virtual-dom-works-under-the-hood</link><guid isPermaLink="true">https://expodev.hashnode.dev/how-react-virtual-dom-works-under-the-hood</guid><dc:creator><![CDATA[Gagan]]></dc:creator><pubDate>Fri, 08 May 2026 21:38:52 GMT</pubDate><content:encoded><![CDATA[<p>React’s Virtual DOM is one of the most talked-about concepts in frontend development.</p>
<p>People often hear:</p>
<blockquote>
<p>“React is fast because of the Virtual DOM.”</p>
</blockquote>
<p>But what does that actually mean?</p>
<p>The Virtual DOM is not magic. It is simply an optimization strategy React uses to efficiently update the UI without making unnecessary changes to the browser’s Real DOM.</p>
<p>To understand how React works internally, we first need to understand the problem React is trying to solve.</p>
<hr />
<h1>The Problem with Direct DOM Manipulation</h1>
<p>The browser’s Real DOM is expensive to update frequently.</p>
<p>Whenever the DOM changes, the browser may need to:</p>
<ul>
<li><p>Recalculate layout</p>
</li>
<li><p>Recompute styles</p>
</li>
<li><p>Repaint elements</p>
</li>
<li><p>Reflow the page structure</p>
</li>
</ul>
<p>Even small changes can become expensive when repeated many times.</p>
<p>For example:</p>
<pre><code class="language-js">const title = document.getElementById("title");
title.innerText = "Updated Title";
</code></pre>
<p>This looks simple, but internally the browser may perform several rendering operations.</p>
<p>If an application updates the DOM repeatedly, performance starts degrading.</p>
<p>This became a major problem in large interactive applications.</p>
<p>React introduced the Virtual DOM to solve this efficiently.</p>
<hr />
<h1>Real DOM vs Virtual DOM</h1>
<table>
<thead>
<tr>
<th>Real DOM</th>
<th>Virtual DOM</th>
</tr>
</thead>
<tbody><tr>
<td>Actual browser DOM</td>
<td>Lightweight JavaScript representation</td>
</tr>
<tr>
<td>Expensive to update</td>
<td>Cheap to create and compare</td>
</tr>
<tr>
<td>Directly changes UI</td>
<td>Calculates changes first</td>
</tr>
<tr>
<td>Managed by browser</td>
<td>Managed by React</td>
</tr>
<tr>
<td>Slower for frequent updates</td>
<td>Optimized for frequent updates</td>
</tr>
</tbody></table>
<p>The Virtual DOM is basically a lightweight JavaScript copy of the UI.</p>
<p>Instead of updating the Real DOM immediately, React first works with the Virtual DOM.</p>
<hr />
<h1>What Exactly is the Virtual DOM?</h1>
<p>Consider this React component:</p>
<pre><code class="language-jsx">function App() {
  return &lt;h1&gt;Hello World&lt;/h1&gt;;
}
</code></pre>
<p>Internally, React converts this into a JavaScript object representation.</p>
<p>Conceptually, it looks something like this:</p>
<pre><code class="language-js">{
  type: "h1",
  props: {
    children: "Hello World"
  }
}
</code></pre>
<p>This object is a Virtual DOM node.</p>
<p>React creates an entire tree of such objects.</p>
<hr />
<h1>Initial Render Process in React</h1>
<p>When a React application loads for the first time, React performs an initial render.</p>
<h2>Step 1: Component Executes</h2>
<pre><code class="language-jsx">function App() {
  return &lt;h1&gt;Hello World&lt;/h1&gt;;
}
</code></pre>
<p>React executes the component function.</p>
<hr />
<h2>Step 2: Virtual DOM Tree is Created</h2>
<p>React creates a Virtual DOM tree.</p>
<pre><code class="language-text">App
 └── h1
      └── "Hello World"
</code></pre>
<hr />
<h2>Step 3: React Creates Real DOM Nodes</h2>
<p>React converts the Virtual DOM into actual browser DOM elements.</p>
<pre><code class="language-html">&lt;h1&gt;Hello World&lt;/h1&gt;
</code></pre>
<p>The browser finally paints the UI on the screen.</p>
<hr />
<h1>Diagram: Initial Render Flow</h1>
<pre><code class="language-text">Component
   ↓
Virtual DOM Tree
   ↓
React Converts It
   ↓
Real DOM
   ↓
Browser Paints UI
</code></pre>
<hr />
<h1>What Happens When State or Props Change?</h1>
<p>Suppose we have a counter component:</p>
<pre><code class="language-jsx">function Counter() {
  const [count, setCount] = useState(0);

  return &lt;h1&gt;{count}&lt;/h1&gt;;
}
</code></pre>
<p>When this runs:</p>
<pre><code class="language-js">setCount(1);
</code></pre>
<p>React starts a re-render cycle.</p>
<p>However, React does not immediately update the browser DOM.</p>
<p>Instead, React follows a process.</p>
<hr />
<h1>How State or Props Change Triggers Re-render</h1>
<p>When state or props change:</p>
<ol>
<li><p>React re-executes the component</p>
</li>
<li><p>A new Virtual DOM tree is created</p>
</li>
<li><p>React compares the old and new trees</p>
</li>
<li><p>It finds the minimum required changes</p>
</li>
<li><p>Only those changes are applied to the Real DOM</p>
</li>
</ol>
<p>This comparison process is called reconciliation.</p>
<hr />
<h1>Creation of a New Virtual DOM Tree</h1>
<p>Suppose the old tree is:</p>
<pre><code class="language-text">h1 → "0"
</code></pre>
<p>After updating state:</p>
<pre><code class="language-text">h1 → "1"
</code></pre>
<p>React now compares these two trees.</p>
<hr />
<h1>What Diffing (Reconciliation) Means</h1>
<p>Diffing is the process of comparing:</p>
<ul>
<li><p>Old Virtual DOM tree</p>
</li>
<li><p>New Virtual DOM tree</p>
</li>
</ul>
<p>React checks:</p>
<ul>
<li><p>Did the element type change?</p>
</li>
<li><p>Did props change?</p>
</li>
<li><p>Did text content change?</p>
</li>
<li><p>Did children change?</p>
</li>
</ul>
<p>Instead of rebuilding the entire UI, React calculates only the differences.</p>
<p>This process is called reconciliation.</p>
<hr />
<h1>How React Finds Minimal Required Changes</h1>
<p>Consider this example.</p>
<p>Old DOM:</p>
<pre><code class="language-html">&lt;h1&gt;0&lt;/h1&gt;
</code></pre>
<p>New DOM:</p>
<pre><code class="language-html">&lt;h1&gt;1&lt;/h1&gt;
</code></pre>
<p>React notices:</p>
<ul>
<li><p>The element type is still <code>h1</code></p>
</li>
<li><p>Only the text content changed</p>
</li>
</ul>
<p>So React updates only the text node.</p>
<p>It does not recreate the entire <code>&lt;h1&gt;</code> element.</p>
<p>This reduces unnecessary DOM operations.</p>
<hr />
<h1>Updating Only Changed Nodes in the Real DOM</h1>
<p>After diffing, React applies the minimal required updates.</p>
<p>Instead of rebuilding the whole UI:</p>
<ul>
<li><p>Only changed nodes are updated</p>
</li>
<li><p>Unchanged elements remain untouched</p>
</li>
</ul>
<p>This makes updates much more efficient.</p>
<hr />
<h1>Example Update Flow</h1>
<h2>Before Update</h2>
<pre><code class="language-text">Virtual DOM
 └── h1 → "0"
</code></pre>
<h2>After State Update</h2>
<pre><code class="language-text">New Virtual DOM
 └── h1 → "1"
</code></pre>
<h2>Diffing Result</h2>
<pre><code class="language-text">Only text content changed
</code></pre>
<h2>Real DOM Update</h2>
<pre><code class="language-html">&lt;h1&gt;1&lt;/h1&gt;
</code></pre>
<hr />
<h1>Why This Approach Improves Performance</h1>
<p>DOM operations are expensive.</p>
<p>JavaScript object operations are comparatively cheap.</p>
<p>React reduces performance cost by:</p>
<ul>
<li><p>Performing comparisons in JavaScript</p>
</li>
<li><p>Avoiding unnecessary DOM updates</p>
</li>
<li><p>Updating only modified elements</p>
</li>
</ul>
<p>Instead of:</p>
<pre><code class="language-text">Many expensive DOM updates
</code></pre>
<p>React performs:</p>
<pre><code class="language-text">Cheap comparisons
+
Minimal DOM updates
</code></pre>
<p>This is the core performance advantage of the Virtual DOM.</p>
<hr />
<h1>High-Level React Render → Diff → Commit Flow</h1>
<p>React’s update lifecycle can be simplified into three major phases.</p>
<h2>1. Render Phase</h2>
<p>React executes components and creates a new Virtual DOM tree.</p>
<pre><code class="language-text">Component Runs
   ↓
New Virtual DOM Generated
</code></pre>
<hr />
<h2>2. Diff Phase (Reconciliation)</h2>
<p>React compares:</p>
<ul>
<li><p>Previous Virtual DOM tree</p>
</li>
<li><p>New Virtual DOM tree</p>
</li>
</ul>
<p>Then calculates the differences.</p>
<pre><code class="language-text">Old Tree vs New Tree
        ↓
 Difference Detection
</code></pre>
<hr />
<h2>3. Commit Phase</h2>
<p>React applies minimal updates to the Real DOM.</p>
<pre><code class="language-text">Apply Minimal Changes
        ↓
 Browser Updates UI
</code></pre>
<hr />
<h1>Full React Update Lifecycle Diagram</h1>
<pre><code class="language-text">State / Props Change
          ↓
Component Re-renders
          ↓
New Virtual DOM Created
          ↓
Diffing / Reconciliation
          ↓
Minimal Changes Calculated
          ↓
Real DOM Updated
          ↓
Browser Paints Changes
</code></pre>
<hr />
<h1>Important Clarification</h1>
<p>The Virtual DOM does not replace the Real DOM.</p>
<p>The browser UI can only be updated through the Real DOM.</p>
<p>The Virtual DOM is simply React’s strategy for deciding:</p>
<ul>
<li><p>What changed</p>
</li>
<li><p>What actually needs updating</p>
</li>
<li><p>What can remain untouched</p>
</li>
</ul>
<p>It acts as an optimization layer between React components and the browser.</p>
<hr />
<h1>Why React Became Popular Because of This</h1>
<p>Before React:</p>
<ul>
<li><p>Developers manually manipulated the DOM</p>
</li>
<li><p>UI updates became difficult to manage</p>
</li>
<li><p>Large applications suffered performance issues</p>
</li>
</ul>
<p>React introduced:</p>
<ul>
<li><p>Declarative UI</p>
</li>
<li><p>Predictable rendering</p>
</li>
<li><p>Efficient updates using reconciliation</p>
</li>
</ul>
<p>Developers only describe what the UI should look like.</p>
<p>React figures out how to update it efficiently.</p>
<hr />
<h1>Final Mental Model</h1>
<p>React follows this process:</p>
<pre><code class="language-text">UI Changes
   ↓
Create New Virtual DOM
   ↓
Compare with Old Virtual DOM
   ↓
Find Smallest Difference
   ↓
Update Only Necessary Real DOM Nodes
</code></pre>
<p>That is the core idea behind how React Virtual DOM works under the hood.</p>
<hr />
<h1>Additional Notes</h1>
<h2>Why Keys Matter in Lists</h2>
<p>When rendering lists:</p>
<pre><code class="language-jsx">{todos.map(todo =&gt; (
  &lt;Todo key={todo.id} /&gt;
))}
</code></pre>
<p>Keys help React identify:</p>
<ul>
<li><p>Which item changed</p>
</li>
<li><p>Which item moved</p>
</li>
<li><p>Which item was removed</p>
</li>
</ul>
<p>Without proper keys, React may perform unnecessary re-renders.</p>
<hr />
<h2>Virtual DOM is Not the Only Optimization Strategy</h2>
<p>Modern frameworks use different rendering approaches.</p>
<p>For example:</p>
<ul>
<li><p>React uses Virtual DOM</p>
</li>
<li><p>Svelte compiles updates directly</p>
</li>
<li><p>SolidJS uses fine-grained reactivity</p>
</li>
</ul>
<p>React’s strength is not only performance.</p>
<p>Its major strengths are:</p>
<ul>
<li><p>Predictable UI updates</p>
</li>
<li><p>Large ecosystem</p>
</li>
<li><p>Reusable component architecture</p>
</li>
<li><p>Developer experience</p>
</li>
</ul>
<hr />
<h1>Conclusion</h1>
<p>The Virtual DOM is React’s smart update mechanism.</p>
<p>Instead of directly manipulating the browser DOM repeatedly, React:</p>
<ol>
<li><p>Creates a Virtual DOM representation</p>
</li>
<li><p>Generates a new tree after updates</p>
</li>
<li><p>Compares old vs new trees</p>
</li>
<li><p>Finds minimal differences</p>
</li>
<li><p>Updates only necessary parts of the Real DOM</p>
</li>
</ol>
<p>This makes React applications more efficient, scalable, and easier to maintain.</p>
<p>Understanding this render → diff → commit lifecycle gives a strong mental model for how React works internally.</p>
]]></content:encoded></item></channel></rss>