Understanding the Basics of Virtual DOM in React

Nikhil Soman Sahu
3 min readApr 12, 2023

--

Understanding the Basics of Virtual DOM in React

React is a popular JavaScript library used for building user interfaces. One of its key features is the Virtual DOM, which is a lightweight representation of the actual DOM (Document Object Model). In this article, we’ll take a closer look at what the Virtual DOM is and how it works in React.

What is the Virtual DOM?

The DOM is a tree-like structure that represents the elements and content of a web page. When a user interacts with a web page, the DOM is updated to reflect the changes made by the user. However, updating the DOM can be slow and expensive, especially for complex applications with a lot of elements.

The Virtual DOM is a lightweight copy of the actual DOM that React uses to track changes to the UI. When the state of a component changes, React creates a new Virtual DOM tree, which is then compared to the previous Virtual DOM tree to determine what has changed. This process is called “reconciliation”.

Once React has determined what has changed, it updates the actual DOM with the new changes. Because the Virtual DOM is a lightweight representation of the actual DOM, this process is much faster and more efficient than updating the entire DOM.

How does the Virtual DOM work in React?

Let’s take a look at an example to see how the Virtual DOM works in React. Suppose we have a simple React component that displays a list of items:

import React, { useState } from 'react';

function MyComponent() {
const [items, setItems] = useState(['Item 1', 'Item 2', 'Item 3']);

function handleClick() {
setItems([...items, 'New Item']);
}

return (
<div>
<button onClick={handleClick}>Add Item</button>
<ul>
{items.map((item) => (
<li key={item}>{item}</li>
))}
</ul>
</div>
);
}

When the user clicks the “Add Item” button, the handleClick function is called, which adds a new item to the items array using the setItems function. This changes the state of the component, which triggers a re-render.

When the component is re-rendered, React creates a new Virtual DOM tree that reflects the updated state of the component. This Virtual DOM tree is then compared to the previous Virtual DOM tree to determine what has changed.

In this case, React determines that a new item has been added to the list. It then updates the actual DOM to reflect this change, without having to update the entire DOM.

Benefits of the Virtual DOM in React

The Virtual DOM provides several benefits for React developers, including:

  • Improved performance: By only updating the parts of the DOM that have changed, React can update the UI much faster and more efficiently.
  • Simplified development: With the Virtual DOM, developers can focus on writing code that describes the state of the UI, rather than worrying about how to update the actual DOM.
  • Compatibility with other libraries: Because the Virtual DOM is a lightweight representation of the actual DOM, it can work with other libraries and frameworks, making it a versatile tool for building web applications.

--

--

Nikhil Soman Sahu

Sr Software Developer | Spring Boot | Flutter | Dart | Java