Sub topic 12
Finding out why a component rendered
This chapter assumes you can write function components, use useState and props, and have recorded a Profiler session as described in the previous chapters. It covers how to turn on render reasons, how to read each reason, and how to use the reasons to find the cause of an extra render.
Why a duration is not enough
The flamegraph tells you which components rendered in a commit and how long each took. It does not tell you why they rendered. A component can render for several different reasons, and each one calls for a different fix:
- A parent re-rendered and React rendered its children too.
- The component's own state changed.
- A hook inside the component changed.
- A context value the component reads changed.
- A prop changed.
If you guess the reason, you may wrap everything in memo and see no improvement. The Profiler can record the actual reason, so you can read it instead.
Turning on render reasons
Render reasons are off by default because recording them adds some overhead. To enable them:
- Open the Profiler tab and click the gear icon to open its settings.
- Under Profiler, check Record why each component rendered while profiling.
- Record a session as usual: click the record button, interact with the app, then stop.
Render reasons apply to sessions recorded after you enable the setting. If you enable it after recording, record again. The reasons are not reconstructed for old sessions.
Select a commit in the commit bar, then click a component in the flamegraph or ranked chart. The right-hand panel shows the render duration and a Why did this render? list for that component in that commit.
The possible reasons
| Reason shown | What it means | Typical fix |
|---|---|---|
| This is the first time the component rendered | The component mounted in this commit. | Usually none. This is expected. |
| Props changed: `name1`, `name2` | One or more props failed a shallow comparison. | Stabilize the prop, or ask whether the prop needs to change at all. |
| State changed | The component's own `useState` or `useReducer` state changed. | Check whether the update was necessary. Move state closer to where it is used. |
| Hooks changed | A hook's value changed, which includes state in a custom hook. | Find the hook in the Components tab and inspect its value. |
| Context changed | A context value the component reads changed. | Split the context, or keep the value stable. |
| The parent component rendered | The parent rendered, and this component is not memoized or its props changed. | Memoize the child, or reduce how often the parent renders. |
The exact wording differs between DevTools versions. The categories are the same.
The "Props changed" reason lists prop names but not the old and new values. To see the values, select the component in the Components tab before and after the interaction, or add a temporary console.log that compares them.
A worked example
The list below is the case from the spine. Item is wrapped in memo, so you would expect that selecting one item re-renders only the item that changed. That is not what happens.
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 with render reasons enabled, click one item, and stop. You get one commit that matches the click. Select List in that commit. Its reason is State changed, because setSelected ran.
Now select any Item. Its reason is Props changed: onSelect. The label prop did not change, but the arrow function (value) => setSelected(value) is created fresh on every render of List. A new function is not equal to the old one under Object.is, so memo sees a changed prop and re-renders every item.
The reason list pointed to one prop name, so you know where to look.
Fixing it and confirming the fix
useCallback keeps the same function reference between renders as long as its dependencies do not change.
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>
);
}The dependency array is empty because setSelected is guaranteed to be stable. Record the same interaction again. The items now appear gray in the flamegraph for that commit, and only List is colored. This second recording confirms the fix. Do not skip it, because a memoization that does not take effect looks the same in the code as one that does.
Cases where the reason surprises you
Props changed, but the values look identical
Objects, arrays, and functions created during render are new on every render, even when their contents match. For example, style={{ color: 'red' }} or options={[1, 2, 3]} will always show as changed. Fix these with useMemo or by moving the constant outside the component.
import { memo, useMemo } from 'react';
const Chart = memo(function Chart({ options }) {
return <pre>{JSON.stringify(options)}</pre>;
});
export function Panel({ threshold }) {
const options = useMemo(() => ({ threshold }), [threshold]);
return <Chart options={options} />;
}The `children` prop
children is a prop. A parent that passes JSX children creates new elements on each render, so a memoized wrapper with children reports Props changed: children. Memoizing the wrapper does not help in this case. Pass the children from a component that does not re-render, or restructure so the changing state lives lower in the tree.
Context changed
A memoized component still re-renders when a context it reads changes, because memo only compares props. If Item reads a context whose value is a new object each time, every consumer re-renders. Memoize the provider's value with useMemo, or split the context into smaller ones.
The parent rendered, but the child is memoized
If a memoized child has unchanged props, it does not render, so no reason is shown for it. The "parent rendered" reason only appears for components that React actually re-rendered.
Development-only double renders
In development, StrictMode renders components twice to expose side effects. DevTools shows one commit, but your console.log calls may appear twice. Do not treat the second log as an extra render. Compare Profiler data against profiling builds if the numbers matter.
Combining reasons with Highlight updates
Highlight updates (from the earlier chapter) is the quick check: it shows borders around every component that renders as you interact. Use it first to spot a suspicious interaction. Then use the Profiler with render reasons on the same interaction to learn the cause. Highlight updates tells you what rendered, and the Profiler tells you why.
Summary of the procedure
- Enable Record why each component rendered while profiling in the Profiler settings.
- Record one specific interaction, then stop.
- Select a commit, then select the component you suspect.
- Read the reason and the prop or hook names it lists.
- Apply the matching fix.
- Record the same interaction again and confirm the component is gray or faster.
Exercise
A memoized component Row receives onDelete={() => remove(id)} from its parent. You record a session with render reasons enabled and click an unrelated button in the parent that changes a counter. Every Row is colored in the flamegraph. What reason do you expect for Row, and how would you fix it?
Show answer
The reason is "Props changed: onDelete". The inline arrow function is a new function on every render of the parent, so memo sees a changed prop. Wrap the handler in useCallback. Because each row needs its own id, either have Row call onDelete(id) itself and pass one shared useCallback handler from the parent, or memoize the handler per row. Then re-record the same interaction and check that the rows are gray.
Exercise
After enabling the setting and recording, a memoized component shows "Context changed". Why does memo not prevent this render, and what are two ways to reduce it?
Show answer
memo only compares props, and a component that reads a context re-renders whenever that context's value changes, regardless of its props. To reduce it, memoize the provider's value with useMemo so it changes only when its inputs change, or split the context into smaller contexts so a component subscribes only to the part it needs.