Virtual DOM
Why virtual DOM?
To avoid re-creation of entire real DOM, when one manipulates DOM elements.The Virtual DOM concept can help in efficient DOM manipulation through diffing and reconciliation.
Diffing is a process of comparing the previous virtual DOM to current virtual DOM and changing only those elements which are different.
Note: List items should have a *key attribute to stop recreating the list items that aren't changed.*
Reconciliation is when virtual DOM syncs with real DOM.
Real or browser DOM looks like the one shown below:
Virtual DOM is nothing but a copy of real DOM which is kept
in memory as a javascript object.
Virtual DOM looks like the one shown below:
//virtual DOMconst virtualDOM = { tagName: 'html', children: [ { tagName: 'head' }, { tagName: 'body', children: [ { tagName: 'div', attributes: { id: 'root' }, children: [ { tagName: 'div', attributes: { class: 'App' }, children: [ { tagName: 'ul', children: [ { tagName: 'li', textContent: 'Tea' }, { tagName: 'li', textContent: 'Juice' }, { tagName: 'li', textContent: 'Water' }, { tagName: 'li', textContent: 'Milk' }, ], }, ], }, ], }, ], }, ], };
Suppose there is a list of four items, and you removed an item or added one, then in real DOM the entire document will be recreated.
//list items <ul> <li key="Tea">Tea</li> <li key="Juice">Juice</li> <li key="Water">Water</li> <li key="Milk">Milk</li> //add item </ul>
The highlighted text shown below in both the images is the recreation.
Manipulating real DOM without using virtual DOM concept
If the same thing is done using virtual DOM only the changed thing will be recreated in the real DOM as shown below.
Original Link: https://dev.to/aasthapandey/virtual-dom-5dc7
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To