liveBook · Manning

Virtual DOM

As explained in React Quickly, Second Edition

A virtual DOM is an in-memory representation of a web document’s UI tree that a rendering library compares before applying changes to the browser DOM.

When a component’s state changes, React needs to bring the browser view up to date. Its virtual DOM is an in-memory representation of the view. React compares what changed before updating the actual browser DOM; you update state rather than changing page elements yourself. section 1.1.1 section 1.1.2

Changing paragraph text

Suppose component state supplies the text of a <p> element. If that text changes, React updates the text in the browser rather than replacing the whole paragraph element. This is especially useful in interfaces with many changing elements. section 1.1.2

Figure 1.2 Once a component has been rendered, if its state changes, it’s compared to the in-memory virtual DOM and re-rendered if necessary.
01-02

Follow a state change from an already-rendered component through the in-memory comparison to a browser update, if one is needed. section 1.1.2

This comparison is called DOM diffing or reconciliation. It also supports component testing without starting a headless browser; Jest and React Testing Library are named options. The details of the reconciliation algorithm are a separate, more advanced topic. section 1.1.1 section 1.1.2

How React Quickly, Second Edition covers it

How deep it goes
Explains in-memory comparison and selective DOM updates, but defers reconciliation algorithm details.
Examples
Uses a paragraph whose state-driven text changes without replacing the element.
What it assumes
Introduces DOM diffing in terms of JavaScript, component state, and the browser view without requiring reconciliation algorithm knowledge.
What you can do after
Apply it via React by updating component state and letting reconciliation handle browser DOM changes.
How it connects
Links component state, DOM diffing, reconciliation, browser DOM updates, and component testing.

Also explained in

Compare how 2 books explain virtual DOM →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of React Quickly, Second Edition. The section links point to the parts of the book it draws on.