Virtual DOM
Virtual DOM is the way React represents render output in memory, compares the previous and next results, and applies only the necessary changes to the real DOM. Developers write as if the whole UI is recalculated, but React narrows the actual updates.
▶Architecture Diagram
🔄 ProcessDashed line animations indicate the flow direction of data or requests
Frequent manual DOM reads and writes are expensive and error-prone. As UI grows more dynamic, asking developers to compute every patch themselves becomes both a performance and correctness burden.
In the jQuery era, developers often selected nodes and patched them directly. That worked for small interactions, but became harder as state and branching grew more complex. React answered that pressure by calculating render output first, then minimizing real DOM work afterward.
When state or props change, React calculates a new render tree. It then compares that tree with the previous one, determines which parts stayed the same and which changed, and commits only the necessary patches to the DOM.
Virtual DOM and refs both touch the border of the real DOM, but they serve different roles. Virtual DOM is React's normal declarative update path. Refs are the escape hatch for directly touching DOM nodes or mutable values outside that path.
Understanding Virtual DOM helps explain why keys matter, why state placement affects rerender scope, and why some component state is preserved or reset when branches change. It is best understood as a mental model of React's update process, not as an API you manipulate directly.