Loading

Quipoin Menu

Learn • Practice • Grow

react / React Virtual DOM
tutorial

React Virtual DOM

Imagine you are a painter working on a massive mural. Every time you want to change a small detail—like the color of a single flower—you don't repaint the entire wall. You only touch up that specific spot. The Virtual DOM works exactly like that for web pages.

What is the DOM?

First, let's understand the actual DOM (Document Object Model). It's a tree-like representation of your web page that JavaScript can interact with. Every time you change something on the page—like updating text, adding a class, or showing a new element—you're manipulating the DOM.

The problem? Manipulating the DOM is slow. When you make a change, the browser has to recalculate styles, re-layout the page, and repaint it. Doing this too often can make your app feel sluggish.

What is the Virtual DOM?

The Virtual DOM is a lightweight, JavaScript representation of the real DOM. It's an object that mirrors the structure of your actual UI, but it lives entirely in memory and has none of the browser's expensive rendering capabilities.

Think of the Virtual DOM as a blueprint or a draft copy of your house. You can make changes on the blueprint freely without tearing down walls. Once you're happy with the changes, you build only those changes in the actual house.

How React Uses the Virtual DOM

Here is the step-by-step process of how React updates the UI:

Step 1: Initial Render
When your React app first loads, it builds a Virtual DOM tree that matches the structure of your components.

Step 2: State Change
Something changes in your app—a user clicks a button, data arrives from an API, a timer ticks. This triggers a re-render of the relevant components.

Step 3: Create a New Virtual DOM
React quickly creates a new Virtual DOM tree reflecting the updated UI based on the new state.

Step 4: Diffing
React compares the new Virtual DOM tree with the previous one. This process is called 'diffing'. It's extremely fast because it's just comparing JavaScript objects, not touching the real browser DOM.

Step 5: Reconciliation
React figures out the minimal set of changes needed to update the real DOM to match the new Virtual DOM. This set of changes is called a 'patch'.

Step 6: Update the Real DOM
Finally, React applies only those specific patches to the real DOM. Only the necessary parts of the page are updated, not the whole thing.

Analogy: The Restaurant Order

Imagine you're at a restaurant and you want to change your order. Without a Virtual DOM (old way), you'd have to tell the waiter to cancel the entire meal and bring a completely new one. With a Virtual DOM (React way), the waiter goes to the kitchen, compares your original order with the new request, and only tells the chef to change the specific items that are different—replace the fries with salad, but keep the steak.

Benefits of the Virtual DOM

  • Performance: By batching updates and only touching the real DOM when necessary, React apps are fast and responsive.
  • Simplicity for Developers: You don't have to worry about manually manipulating the DOM or optimizing updates. You just tell React what the UI should look like, and it handles the rest.
  • Cross-Platform: The Virtual DOM concept allows React to work not just in the browser (ReactDOM), but also on mobile (React Native), where it interacts with native views instead of HTML elements.

Visual Example

Let's say you have a list:
<ul>
  <li>Apple</li>
  <li>Banana</li>
  <li>Orange</li>
</ul>

Now you change the second item to "Mango" and add a new item "Grape" at the end:
<ul>
  <li>Apple</li>
  <li>Mango</li> <span style="color: #6a9955;"><!-- Changed -->
  <li>Orange</li>
  <li>Grape</li> <span style="color: #6a9955;"><!-- New -->
</ul>

Without Virtual DOM, the browser might re-render the entire list. With React's Virtual DOM, it will detect that only the second list item's text changed and a new fourth item was added. It will then update only those two specific parts in the real DOM.

Two Minute Drill

  • The Virtual DOM is a lightweight JavaScript copy of the real DOM, kept in memory.
  • When state changes, React creates a new Virtual DOM tree, compares it with the previous one (diffing), and calculates the minimal updates needed.
  • This process is called reconciliation.
  • React then applies only the calculated patches to the real DOM, making updates efficient.
  • The Virtual DOM is why React apps can be fast and responsive even with complex, frequently updating UIs.
  • As a developer, you don't need to manually manipulate the DOM – you just update the state, and React handles the rest.

Need more clarification?

Drop us an email at career@quipoinfotech.com