![]()
Once the Virtual DOM is created, React compares this new representation with a snapshot of the previous version of the virtual DOM
to see exactly which elements have changed.
Once the difference is known, React updates only those objects that differ on the actual DOM and the browser re-paints the screen.
The next time state or props changes for a component in the application, a new virtual DOM tree of React elements will be created
and the process will repeat.
The process of checking the difference between the new Virtual DOM tree and the old Virtual DOM tree is called diffing. Diffing is
accomplished by a heuristic O(n) algorithm. During this process, React will deduce the minimum number of steps needed to update
the real DOM, eliminating unnecessary costly changes. This process is also referred to as reconciliation.
React implements a heuristic O(n) algorithm based on two assumptions:
1.Two elements of different types will produce different trees.
2.The developer can hint at which child elements may be stable across different renders with a key prop."
↥
Q. How Diff Algorithm is implemented in Reactjs?
The main work of a diff algorithmis to find a heuristic to change anything from a state to another. When diffing two trees, React
first compares the two root elements. The behavior is different depending on the types of the root elements.
1. Elements Of Different Types:
Whenever the root elements have different types, React will tear down the old tree and build the new tree from scratch. When
tearing down a tree, old DOM nodes are destroyed. Component instances receive componentWillUnmount().
When building up a new tree, new DOM nodes are inserted into the DOM. Component instances
receive UNSAFE_componentWillMount() and then componentDidMount(). Any state associated with the old tree is lost.
2. DOM Elements Of The Same Type:
When comparing two React DOM elements of the same type, React looks at the attributes of both, keeps the same underlying DOM
node, and only updates the changed attributes.
Example: By comparing these two elements, React knows to only modify the className on the underlying DOM node.
<divclassName="before" title="React JS" />
<divclassName="after" title="React JS" />
3. Component Elements Of The Same Type:
When a component updates, the instance stays the same, so that state is maintained across renders. React updates the props of the
underlying component instance to match the new element, and
calls UNSAFE_componentWillReceiveProps(),UNSAFE_componentWillUpdate() and componentDidUpdate() on the underlying instance.
Recursing On Children
By default, when recursing on the children of a DOM node, React just iterates over both lists of children at the same time and
generates a mutation whenever there's a difference.
For example, when adding an element at the end of the children, converting between these two trees works well:
<ul>
<li>first</li>
<li>second</li>
</ul>
<ul>
<li>first</li>
<li>second</li>
<li>third</li>
</ul>
React will match the two <li>first</li> trees, match the two <li>second</li> trees, and then insert the <li>third</li> tree.
Keys