On each update React (1) re-runs the affected components to produce a new element tree, (2) diffs it against the current tree using heuristics that make diffing O(n) instead of the O(n³) general tree-diff problem (different type means replace the subtree, same type means update props and recurse, and keys match list children), and (3) commits the collected changes to the real DOM in one batch. Benefits: a declarative model, batched and minimal DOM writes, cross-platform renderers, and interruptible rendering. Downsides: memory and CPU overhead for building and diffing trees, unnecessary re-renders unless you structure or memoize, and the heuristics can force remounts when structure changes.
1. Trigger and render. setState schedules an update on that component's fiber. React walks from the root, skipping unchanged subtrees, and calls the components that need re-rendering. They return new elements.
2. Reconcile (diff). React compares new elements with the existing fibers, child by child:
// Before // After
<div> <div>
<Header title="A" /> <Header title="B" /> // same type: update props, re-render Header
<p>Hello</p> <section>Hello</section> // different type: destroy <p> subtree, create <section>
</div> </div>
div → div): keep the DOM node and patch only the changed attributes.The diff produces a list of effects: insertions, updates, deletions and moves.
3. Commit. React applies all DOM mutations synchronously in one pass, attaches refs, runs layout effects, and then (after paint) runs passive effects. Because writes are batched, the browser recalculates layout once rather than after each change.
A fully optimal tree diff (minimum edits, including moving subtrees across levels) is cubic. React assumes:
Real UIs rarely move subtrees across levels, so these rules work well and make diffing linear.
memo, or the React Compiler.Spotted a mistake or a rights concern? Report an issue