Sub topic 4
The Render Phase and the Commit Phase
This chapter assumes you know JavaScript functions, basic components, props and JSX, and useState. It also assumes you know what the browser DOM is. It explains the two stages React runs between a state change and an updated screen.
Two phases, two jobs
When state changes, React does not touch the DOM right away. It works in two phases.
- Render phase: React calls your component functions and collects the elements they return. It then compares the new result with the previous one. This phase does not change the screen.
- Commit phase: React applies the differences to the DOM, then runs layout effects and, later, passive effects.
"Render" in React means "call your component function". It does not mean "paint pixels". A component can render many times without any DOM change, if its output is the same.
Seeing the render phase
Here is a component with a log statement in its body.
import { useState } from 'react';
function Counter() {
const [count, setCount] = useState(0);
console.log('Counter rendered with count', count);
return (
<button onClick={() => setCount(count + 1)}>
Clicked {count} times
</button>
);
}On each click, setCount schedules a re-render. React calls Counter again with the new count. The log line appears during the render phase, before the DOM text changes. At that moment the button on screen still shows the old number.
What the render phase must do
The render phase only calculates. Your component should return the same output for the same props and state, and it should not change anything outside itself. Do not write to the DOM, start timers, or modify variables defined outside the component during render.
This rule exists because React may call a component more than once, pause it, or throw the result away. A render that causes side effects would then run those effects at unpredictable times.
What the commit phase does
Once React knows what changed, the commit phase runs. It is synchronous, so the DOM is never left half updated. In order, React:
- Applies the changes to the DOM, such as updating text or adding and removing nodes.
- Updates refs, so
ref.currentpoints at the new nodes. - Runs layout effects (
useLayoutEffect) before the browser paints. - Lets the browser paint.
- Runs passive effects (
useEffect) after the paint, in most cases.
If nothing changed between the two trees, React skips the DOM work. A component that re-rendered but produced identical output causes no DOM writes.
| Question | Render phase | Commit phase |
|---|---|---|
| Calls your components? | Yes | No |
| Touches the DOM? | No | Yes |
| Can be repeated or discarded? | Yes | No |
| Where do effects run? | Never | Yes, after DOM updates |
Common errors
- Assuming a log means a DOM update. A
console.login the body shows that the function ran. It does not show that the DOM changed. - Reading the DOM in render. The DOM still holds the previous result during the render phase. Read measurements in a layout effect instead.
- Side effects in the body. Fetching data or subscribing in render can run many times. Put this work in an effect or an event handler.
- Expecting state to change immediately. After
setCount(count + 1), the variablecountin that same render keeps its old value. The new value appears in the next render.
Check your understanding
Exercise
A component re-renders after a state change, but its returned elements are identical to the previous render. Did the component function run? Did the DOM change?
Show answer
Yes, the component function ran, because that is the render phase. The DOM did not change, because reconciliation found no differences, so the commit phase had nothing to apply.
Exercise
Why is it a problem to write to a global variable inside a component body?
Show answer
The render phase can run more than once, pause, or be discarded. A write during render would then happen at unpredictable times and could leave the app in an inconsistent state. Side effects belong in event handlers or effects.