~/alexsh.blog

$ cat react-internals/04-commit-phase.md

Commit Phase

February 9, 20261 min readpart 04/13

The commit phase is synchronous — once started, it runs to completion. This prevents partially-updated UI. Three sub-phases:

1. Before Mutation

Calls getSnapshotBeforeUpdate for class components. Schedules passive effects (useEffect) but doesn't flush them yet.

2. Mutation

Actual DOM operations: insertions, updates, deletions. Runs componentWillUnmount, useLayoutEffect cleanups, and detaches refs from old nodes.

3. Layout

Runs componentDidMount/componentDidUpdate, fires useLayoutEffect callbacks synchronously (before paint), and attaches refs to new DOM nodes.

After layout, React swaps root.current to the WIP tree. Then asynchronously, after the browser paints, it flushes passive effects.

diagramtext
Render Phase (interruptible)          Commit Phase (synchronous)
┌─────────────────────────┐    ┌────────────────────────────────────┐
│ beginWork → completeWork│───→│ beforeMutation → mutation → layout │
│ (builds WIP tree)       │    │ (applies changes to DOM)           │
└─────────────────────────┘    └──────────────┬─────────────────────┘
                                              │
                                              ▼  (async, after paint)
                                     Passive Effects (useEffect)

Further reading