DEV Community

Cover image for React Virtual DOM and diffing- algorithm Simplified
Nasirul Islam
Nasirul Islam

Posted on

React Virtual DOM and diffing- algorithm Simplified


React is JavaScript open-source library. Using which we can make various interactions in the UI.


React gives us a markup syntax of its own, with which we can create a react element, called JSX. That is, JavaScript XML. It looks like HTML but not HTML.

React generates HTML code with JSX. Although we write the syntax of JSX inside the JavaScript file, although JavaScript does not understand JSX, we have to use a transpiler. For example Babel. Babel converts JSX codes to JavaScript.

Virtual DOM:

React creates elements for itself using its createsElements () function. By combining all the elements, he creates a separate dom inside himself. This dom is called Virtual Dom. React's virtual dom basically renders as much as the browser's dom has changed.

We can imagine the Virtual Dom as a simple tree. The different notes of which are one component. Whenever we change the state of a component, a tree is created first. Where the modified component and its child components are reconstructed. That is, React has two representations of the virtual dom. One is the state before it, and the other is the state after the change. React compares these two conditions.


React uses the "Diff" algorithm to do this comparison. With that, React can understand which places have changed. React then changes only the part of the dom where the change was made.


Attempts have been made to simply explain the virtual DOM and diffing-algorithm without any explanation.

Thanks for reading

Reach out to me on:

Discussion (0)