Sub topic 9
When Effects Run Relative to Rendering and Commit
This chapter assumes you know JavaScript functions, basic React components and props, and useState. It also assumes you know the browser DOM. You should have read the chapter on the render phase and the commit phase.
Effects run after the commit
A render is a calculation. It must not change anything outside the component. An effect is where you do that kind of work, such as subscribing to a service, starting a timer, or updating document.title. You declare an effect with useEffect.
React runs the effect after the commit phase. By then the DOM is updated, and in most cases the browser has painted the new screen. The order for one update is:
- React calls your component (render phase).
- React updates the DOM (commit phase).
- The browser paints.
- React runs the cleanup of the previous effect, then the new effect.
A worked example
import { useEffect, useState } from 'react';
function Timer({ step }) {
const [seconds, setSeconds] = useState(0);
console.log('render, seconds =', seconds);
useEffect(() => {
console.log('effect runs, step =', step);
const id = setInterval(() => setSeconds(s => s + step), 1000);
return () => {
console.log('cleanup, step =', step);
clearInterval(id);
};
}, [step]);
return <p>{seconds}</p>;
}On the first render the console shows render, then effect runs. The log line in the component body runs during the render phase. The log line in the effect runs after the commit.
When step changes from 1 to 2, you see render, then cleanup, step = 1, then effect runs, step = 2. The cleanup always uses the values from the render that created it. React clears the old interval before it starts the new one.
When only seconds changes, the component renders again, but the effect does not run. The dependency array contains only step, and React compares each entry with its previous value using Object.is.
The dependency array
| Dependency array | When the effect runs |
|---|---|
| Omitted | After every commit |
| `[]` | After the first commit only (and cleanup on unmount) |
| `[a, b]` | After the first commit, and after any commit where `a` or `b` changed |
The array is not a performance trick. It tells React which values the effect reads. If the effect uses a value that is missing from the array, the effect sees a stale value from an older render.
Layout effects and the paint
useEffect normally runs after paint, so the user may see one frame of the committed DOM before the effect runs. If your effect measures the DOM and then updates state to reposition something, that frame can show as a flicker. useLayoutEffect has the same signature but runs after the DOM update and before the browser paints. Use it only when you must measure or adjust layout before the user sees the screen. It blocks painting, so keep it short.
Common errors
- Setting state in an effect with no dependency array. The state update causes a render, the render causes the effect, and the effect sets state again. This loops forever.
- Forgetting cleanup. A subscription or timer that you never clear keeps running after the component unmounts or after its inputs change.
- Using an object or function created during render as a dependency. A new object is created on every render, so
Object.issees a change each time and the effect re-runs every commit. - Doing derived calculations in an effect. If you can compute a value from props and state during render, do that. An effect adds an extra render.
Exercise
A component renders with useEffect(() => { document.title = name; }, [name]);. The parent re-renders and passes the same name string. Does the component function run again, and does the effect run again?
Show answer
The component function can run again, because a parent re-render re-renders its children by default. The effect does not run, because Object.is finds name unchanged and React skips the effect.
Exercise
In what order do these appear in the console when step changes: the render log, the cleanup log of the previous effect, the new effect log?
Show answer
The render log comes first, because rendering is the first phase. The cleanup of the previous effect comes next, and the new effect runs last. Both of those happen after the commit.