Sub topic 2
How React renders and commits
This chapter explains what React does between a state change and pixels on screen. It also explains why that work can be slow. It assumes you can write function components with props, state, and hooks, and that you know JavaScript closures and array methods. You should be able to open the browser developer tools. Later chapters use this model to read profiler output.
Three steps: trigger, render, commit
React updates the screen in three steps.
- Trigger. Something schedules work. The first mount triggers a render. After that, a state setter (
setStatefromuseState, ordispatchfromuseReducer) triggers one for the component that owns the state. - Render. React calls your component functions to find out what the UI should look like now.
- Commit. React applies the differences to the DOM, then runs effects.
In React, the word "render" means calling your function. It does not mean drawing. A component can render many times and never change the screen.
What happens in the render phase
During the render phase, React starts at the component whose state changed. It calls that function. The function returns JSX, which is a tree of plain objects called elements. Each element describes what you want, such as "a <ul> containing these <Row> elements". React then calls each child component the same way, all the way down.
React compares the new element tree with the previous one. This comparison is called reconciliation. It marks what must change: a text node, an attribute, a new or removed child. React matches elements by type and position, and by key inside lists. If the type at a position changes, React discards the old subtree and its state.
The render phase has two properties that matter for profiling.
- It must be pure. A component should compute its output from props, state, and context, and do nothing else. Do not mutate variables outside the function, and do not fetch data in the render body.
- It costs JavaScript time. Every component function that runs, and every
.mapor.filterinside it, uses the main thread. A slow render blocks clicks and typing.
The render phase does not touch the DOM. In concurrent features such as useTransition, React can pause or throw away a render in progress. For that reason a render function must be safe to call more than once. In development, <StrictMode> calls your components twice on purpose so that impure code shows up.
What happens in the commit phase
React applies the marked changes to the DOM in one synchronous pass. The commit cannot be interrupted. If nothing changed in the output, React changes nothing in the DOM.
The commit also runs your effects. Their timing is easy to get wrong.
| Step | When it runs | What runs |
|---|---|---|
| DOM mutation | During the commit | React inserts, updates, and removes DOM nodes. |
| Ref attachment | During the commit | `ref.current` is set. |
| Layout effects | Right after the DOM changes, before paint | `useLayoutEffect` callbacks. |
| Paint | After the commit | The browser draws the new pixels. |
| Passive effects | Usually after paint | `useEffect` callbacks. |
A useLayoutEffect that does heavy work delays the paint, because the browser has to wait for it. A useEffect that sets state starts a new trigger, which leads to another render and commit.
Seeing renders happen
A console.log in the component body shows when React calls it. Here is a complete example you can drop into a Vite or Create React App project.
import { useState } from "react";
function Child({ label }) {
console.log("render Child", label);
return <p>{label}</p>;
}
export default function Parent() {
const [count, setCount] = useState(0);
console.log("render Parent", count);
return (
<div>
<button onClick={() => setCount(count + 1)}>Clicked {count} times</button>
<Child label="I take no props that change" />
</div>
);
}Click the button once. The console prints render Parent 1 and render Child .... The Child function ran again even though its props are identical.
This is the rule that explains most slow React apps: when a component renders, React renders all of its children too, whether or not their props changed. Props are not compared by default. React then compares the output, finds that Child produced the same paragraph, and leaves the DOM alone. The render still cost time, and the commit did nothing.
Under StrictMode in development you will see each log line twice. That is the double render described above. It does not happen in production.
When children are skipped
React skips a child in two cases.
- The child is wrapped in
React.memoand its props are shallowly equal to the last render. Memoization has its own chapter. - The child element was created by a parent higher up and passed in as
children. React sees the same element object and reuses the previous result.
The second case is useful and often overlooked:
import { useState } from "react";
function Child({ label }) {
console.log("render Child", label);
return <p>{label}</p>;
}
function Counter({ children }) {
const [count, setCount] = useState(0);
return (
<div>
<button onClick={() => setCount(count + 1)}>Clicked {count} times</button>
{children}
</div>
);
}
export default function App() {
return (
<Counter>
<Child label="created by App" />
</Counter>
);
}App creates the <Child> element. Clicking the button re-renders Counter but not App, so the element object is the same one as before. Child does not log again.
Why extra renders cost time
An extra render has three costs.
- Function execution. The component body runs, including any sorting, filtering, or formatting inside it.
- Reconciliation. React compares the returned elements with the previous ones. Large lists make this slow.
- Blocked main thread. JavaScript, layout, and input handling share one thread. A 200 ms render means a 200 ms delay before the next keystroke is processed. Users notice delays above roughly 100 ms.
A render that produces no DOM change is still real cost. Do not use "the DOM did not change" as a sign that nothing is wrong.
Also look at cost in the other direction. A commit that changes many DOM nodes triggers browser layout and paint work. The React DevTools Profiler measures render time and commit timing, but it does not measure the browser's paint time. The Performance panel, covered in a later chapter, does.
Common mistakes
- Thinking a render means a DOM update. It means React called your function. The commit decides what changes in the DOM.
- Expecting state to change immediately after calling the setter.
setCount(count + 1)schedules a render. Thecountvariable in the current function call keeps its old value. React also batches several updates inside one event handler into a single render. - Mutating state.
items.push(x); setItems(items)passes the same array reference, so React may bail out. Create a new array:setItems([...items, x]). - Side effects in the render body. Subscriptions, timers, and network calls belong in event handlers or
useEffect. Under StrictMode they run twice. - Creating components inside components. A function component declared inside another component is a new type on every render. React discards the old subtree and its state each time, then remounts it.
// Bad: Inner is a new function on every render, so it remounts every time.
function Outer() {
function Inner() {
return <input />;
}
return <Inner />;
}React also skips work when you set state to a value that is Object.is-equal to the current value. It may still call the component once more before it bails out, so a log line can appear one extra time. That is expected.
Where this model leads
The Profiler records every commit. For each one it shows which components rendered and how long each took. The model in this chapter tells you how to read that data.
- A component with a long bar spends time in its own function, which is a render cost problem.
- A component that appears in many commits without changed props is re-rendering because its parent did. The fix is usually to restructure state or add memoization.
- A commit that is slow but has few rendered components points away from React, toward effects or the browser's own work.
Concurrent features also build on the split between phases. Because the render phase has no side effects, React can interrupt it so that urgent updates such as typing are handled first.
Exercise
In the first example in this chapter, Parent holds the count state and renders <Child label="..." />. Clicking the button logs render Child again. Name two different changes that would stop Child from logging on each click, and say which phase of React's work each change avoids.
Show answer
One change is to wrap Child in React.memo. Its props are shallowly equal on each click, so React skips calling it. The other is to move the button and count into their own component, or to pass <Child /> in as children from a higher component, so the parent that owns count no longer creates or contains Child. Both changes avoid the render phase for Child. The commit has nothing to do for Child either way, because its output never changed.