Sub topic 2
How React Renders and Commits
This chapter assumes you can write function components with props, state, and hooks. You should also know how to open the browser console. It explains the two phases behind every update, because the Profiler reports its results in those terms.
Render is not the same as updating the screen
In React, "render" means calling your component function to find out what it should display. It does not mean drawing pixels. An update has two phases:
- Render phase. React calls components and collects the elements they return. It compares the new result with the previous one. This phase only runs JavaScript and touches nothing on screen.
- Commit phase. React applies the differences it found to the DOM. Then it runs layout effects and, after the browser paints,
useEffectcallbacks.
What triggers a render
A render starts in one of two ways. The first is the initial mount. The second is a state update, from setState or a dispatch, in a component that is already mounted.
After that, rendering cascades down the tree. When a component renders, React renders all of its children by default. This happens even if their props did not change. Props only matter if you opt out, which later chapters cover.
import { useState } from "react";
function Child() {
console.log("Child rendered");
return <p>I never change</p>;
}
export default function Parent() {
const [count, setCount] = useState(0);
console.log("Parent rendered");
return (
<>
<button onClick={() => setCount(count + 1)}>Clicked {count}</button>
<Child />
</>
);
}Each click logs both "Parent rendered" and "Child rendered". Child returns the same output every time, yet its function still runs. In development, Strict Mode calls each component twice on purpose, so you may see doubled logs.
Why extra renders cost time
A render that produces the same output as before leaves the DOM alone, so the commit phase does little or nothing. The render phase work was still done, though. Every extra render costs:
- the time to run the component function and every hook inside it
- the time to compare the old and new elements
- any expensive calculation written directly in the body
For a small component this is microseconds. For a large subtree, or a component that sorts or filters thousands of items, the cost adds up. A render that takes longer than a frame (about 16 ms at 60 frames per second) makes input and animation feel sluggish. The main thread is busy and cannot respond.
The commit phase can be costly too. Changing many DOM nodes forces the browser to recalculate layout and repaint. You will usually see the biggest wins from skipping unnecessary renders, but keep both phases in mind.
Common mistakes in this mental model
- Thinking a render means the DOM changed. It does not. Many renders end with no DOM changes.
- Thinking a re-render means state was lost. React keeps state for a component that stays in the same position in the tree.
- Doing side effects in the render body. Rendering must be pure. Put side effects in event handlers or
useEffect.
Exercise
In the Parent and Child example, you click the button once. How many times does Child's function run in a production build, and does the DOM for Child change? Explain.
Show answer
Child's function runs once per click, because rendering Parent renders its children by default. The DOM for Child does not change, since the new output matches the old output. React skips DOM updates for it in the commit phase, but the render work was still done. In development with Strict Mode you may see the function called twice per render.