Virtual DOM in React. A brief overview.

Imagine you're working on a website, and you want to update something on the page, like changing some text or adding a new element. Your web page is actually a giant family tree with elements like div, p, img, table, span and many more, all interconnected in a tree-like hierarchical structure. This is the real DOM structure. Now, managing and updating this DOM tree can be slow because the browser has to redraw the whole page every time something changes. This is where React brings in the Virtual DOM concept.
The Virtual DOM is like a lightweight copy of the real DOM. When you make changes in your React app, those changes are first applied to the Virtual DOM. This is super fast because it’s just a JavaScript object.
Here’s a quick rundown of how it works:
Initial Render: When your React app first loads, React creates a Virtual DOM that represents what the UI should look like.
State Changes: When the state of your app changes (for example, a user clicks a button), React updates the Virtual DOM instead of the real DOM.
Diffing: React then compares the updated Virtual DOM with the previous version of the Virtual DOM. This process is called “diffing.” This algorithm compares the new Virtual DOM with the previous version to identify what exactly has changed. The algorithm looks for differences in nodes (elements) and updates only the ones that have changed.
Reconciliation: After determining the differences, React updates only the parts of the real DOM that have changed. This is much more efficient than updating the whole DOM, so your app stays fast and responsive.
In short, the Virtual DOM helps React manage updates efficiently, making your app faster and more performant. It’s like having a smart assistant that only makes the necessary changes instead of redoing everything from scratch.
Hope this blog was helpful! This one is a short and overview on how the virtual DOM operates in React. 😊
Subscribe to my newsletter
Read articles from shohanur rahman directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
