React Reference ยท Concepts and Pitfalls
Render and commit
Understand the two phases of every React update: render calculates what the screen should look like, commit applies changes to the DOM and runs effects.
Every time something changes on screen, React goes through the same three steps: a trigger, a render and a commit. The trigger is the first root.render() call or a state update. Rendering means calling your component functions to find out what the UI should look like. Committing means applying the difference to the DOM and then running effects.
The most useful thing to remember is that "render" does not mean "paint" and does not mean "touch the DOM". Rendering is only calculation. A component can render many times and cause no DOM change at all. Only the commit phase writes to the page, and the browser paints after that.
// 1. trigger: setCount(1) or root.render(<App />) // 2. render: React calls App(), Child(), ... and compares the results // 3. commit: React updates the DOM, then runs layout effects, then effects
Try it
This example logs every step so you can see the order. Press the button and read the console: all the render logs come first (parent, then child), then the DOM is updated, then the effects fire (child first, then parent).
Rendering is cheap to repeat and commit is expensive, so React tries to avoid touching the DOM. The next example counts real DOM writes with a MutationObserver. The parent re-renders on every click, but only the text that actually changed is written.
On the first click the parity text changes, so there is one write. The static paragraph is never rewritten, even though its component function ran again.
Syntax
This page explains an idea, so here are the phases in more detail.
| Phase | What happens | What you may do there |
|---|---|---|
| Trigger | The initial root.render(), or a call to a state setter, dispatch, or a context change that affects a mounted component. |
Call setters from event handlers, effects and async callbacks. |
| Render | React calls your components top to bottom, using the new props and state, and builds a tree of elements. It may call a component more than once, pause, or throw the work away. | Calculate JSX from props, state and context. Nothing else: no mutation, no network calls, no DOM access. |
| Commit | React applies the minimum set of changes to the DOM, attaches or detaches refs, runs layout effects synchronously, lets the browser paint, then runs effects. | Read layout in useLayoutEffect; subscribe, fetch and synchronize in useEffect. |
The initial render is special only in that there is nothing to compare with, so React creates all the DOM nodes.
Usage notes
- Rendering must be pure. Because React may render a component twice (
StrictModedoes it on purpose in development) or discard a render, side effects during render are bugs. See Components must be pure. - A parent render re-renders its children by default, even when their props are identical, unless a child is wrapped in
memoor the compiler memoizes it. A re-render is not a problem by itself; it only matters when it is slow. - Effects run after the paint, layout effects before it. If an effect changes state, React renders and commits again.
- Cleanups run before the next setup. On an update, React runs the previous effect's cleanup before the new effect body.
- Refs are attached during commit, which is why
ref.currentisnullwhile a component first renders. - State updates are queued, not applied immediately. See State batching and State as a snapshot.
- "Why did it render?" is answered by the trigger: a state change in the component, a re-render of its parent, or a context value it reads.