Sub topic 12
Finding out why a component rendered
This chapter assumes you can write React function components, use useState and props, and have recorded a Profiler session as covered in the previous chapters. Here you will learn to read the Profiler's render reasons and match each reason to a fix.
Why the reason matters
The flamegraph shows that a component rendered and how long it took. It doesn't tell you what to change. The render reason does. A component can re-render for several different causes, and each cause has a different fix.
Turn on render reasons
The Profiler only records render reasons if you ask for them.
- Open the Profiler tab and click the gear icon.
- In the Profiler settings, check "Record why each component rendered while profiling."
- Record a session, then select a commit.
- Click a component in the flamegraph. The right-hand pane lists why it rendered.
Recording reasons adds some overhead, so turn the setting off when you only need timings.
The common reasons
| Reason shown | What it means | Typical fix |
|---|---|---|
| Props changed: `onSelect` | A named prop is not equal to its previous value | Stabilize the value, for example with `useCallback` |
| State changed | The component's own `useState` or `useReducer` value changed | Usually expected. Check whether the update was needed |
| Hooks changed | A hook value, such as context, changed | Narrow what the component reads |
| The parent component rendered | The parent re-rendered and this component was not memoized | Wrap in `memo` if the render is costly |
Greyed-out bars in the flamegraph did not render in that commit. If a component you expected to be grey is colored, check its reason.
A worked example
This list has a memoized Item. Selecting an item should only update the "Selected" text and the clicked row.
import { memo, useState } from 'react';
const Item = memo(function Item({ label, onSelect }) {
return <li onClick={() => onSelect(label)}>{label}</li>;
});
export function List({ items }) {
const [selected, setSelected] = useState(null);
return (
<div>
<p>Selected: {selected ?? 'none'}</p>
<ul>
{items.map((label) => (
<Item
key={label}
label={label}
onSelect={(value) => setSelected(value)}
/>
))}
</ul>
</div>
);
}Record a session, click one item, and stop recording. Every Item is colored in the commit. Select one and the Profiler reports "Props changed: onSelect".
The cause is the inline arrow function. List re-renders when selected changes, and each render creates a new function. memo compares props with Object.is, sees a different function, and renders Item again. The label prop did not change, so the reason names only onSelect.
Apply the fix and verify
useCallback returns the same function between renders when its dependencies are unchanged. The setter from useState is stable, so the dependency array can be empty.
import { memo, useCallback, useState } from 'react';
const Item = memo(function Item({ label, onSelect }) {
return <li onClick={() => onSelect(label)}>{label}</li>;
});
export function List({ items }) {
const [selected, setSelected] = useState(null);
const handleSelect = useCallback((value) => setSelected(value), []);
return (
<div>
<p>Selected: {selected ?? 'none'}</p>
<ul>
{items.map((label) => (
<Item key={label} label={label} onSelect={handleSelect} />
))}
</ul>
</div>
);
}Profile the same click again. Items whose label did not change should now be grey, with a note that the component did not render. This confirmation step matters. A fix that doesn't change the Profiler output hasn't fixed the problem you measured.
Gotchas
memodoes nothing for a component that always receives a new object, array, or function prop.- Adding
useCallbackwithoutmemoon the child gives no benefit. The child still renders when its parent does. - Don't memoize everything. Do it where the Profiler shows a render that costs noticeable time.
Exercise
In the Profiler, a memoized component shows "Props changed: style". The parent passes style={{ color: 'red' }} inline. Why does this happen, and what is one way to fix it?
Show answer
The inline object literal is a new object on every parent render, so memo's shallow comparison sees a different value even though the contents are the same. Fix it by moving the object outside the component as a constant, or by creating it with useMemo, so the same reference is passed each time. Then profile again to confirm the component is no longer colored.