React › Virtual DOM & Reconciliation
What is the virtual DOM in React, and why does React use it?
BeginnerAnswer
The virtual DOM is a lightweight, plain JavaScript representation of what the actual page should look like, kept in memory by React. When state changes, React first updates this lightweight virtual version, compares it to the previous version, and only then makes the minimum necessary changes to the real, slower browser DOM, making updates much faster overall.
// You never write this directly, but conceptually React creates something like:
const virtualElement = {
type: 'button',
props: { children: 'Click Me' },
};
// React compares this against the previous version before touching the real DOMReal-world example A large data table updates only the specific cells that actually changed on the real page, instead of redrawing the entire table every time, thanks to React comparing virtual DOM versions before touching the real, slower browser DOM.