Sub topic 6
Finding out why a component rendered
This chapter assumes you can record a session in the React DevTools Profiler and read the flamegraph, both covered earlier. You should also know that a parent re-render re-renders its children by default.
Why the reason matters
The flamegraph shows which components rendered and how long they took. It does not show why they rendered. The reason decides the fix. A component that rendered because its state changed is doing necessary work. A component that rendered only because its parent did may be wasted work. Applying memo without knowing the reason often changes nothing.
Turn on render reasons
DevTools does not record reasons by default, because tracking them adds overhead.
- Open the Profiler tab and click the gear icon.
- Under the Profiler settings, check "Record why each component rendered while profiling".
- Record a session as usual, then stop it.
- Click a component in the flamegraph. The sidebar shows "Why did this render?" followed by the reasons.
Components drawn in gray did not render in the selected commit. Use the commit selector at the top to step through commits and compare.
The reasons you will see
| Reason in the sidebar | What it means | Typical response |
|---|---|---|
| Props changed: name | A prop is no longer equal to the previous value. | Check whether the value really changed or only its identity. |
| State changed / Hooks changed | A hook inside the component updated. | Usually necessary. Consider moving the state down. |
| Context changed | A context the component reads has a new value. | Split the context or memoize its value. |
| The parent component rendered | Nothing changed for this component. Its parent re-rendered. | A candidate for `memo` or for restructuring. |
A worked example: props that change identity
The sidebar often reports "Props changed: options" even though the contents are the same. JavaScript compares objects, arrays, and functions by reference, and an inline literal creates a new reference on every render.
import { memo, useState } from "react";
const Chart = memo(function Chart({ options }) {
return <pre>{JSON.stringify(options)}</pre>;
});
export default function Dashboard() {
const [count, setCount] = useState(0);
return (
<>
<button onClick={() => setCount(count + 1)}>Clicked {count}</button>
{/* New object on every render, so memo never skips Chart */}
<Chart options={{ color: "blue" }} />
</>
);
}Profile a click on the button. Chart renders each time, and the sidebar says "Props changed: options". The fix is to give the prop a stable identity. Define the object outside the component, or wrap it in useMemo.
const chartOptions = { color: "blue" };
// inside Dashboard:
<Chart options={chartOptions} />Record again. Chart now appears gray on the click commits.
Common errors
- Reading reasons in a recording made without the setting. The sidebar then shows no reason. Enable the setting and record again.
- Treating "Hooks changed" as the cause when the hook is a custom one. The reason points to a hook index, not a name. Open the Components tab to see which hook holds the value that changed.
- Fixing a prop that was not the cause. If the reason is "The parent component rendered", stabilizing props alone will not help until the component is wrapped in
memo. - Chasing every extra render. A cheap component that renders often is not a problem. Fix the ones with a large
actualDuration.
Exercise
In the Profiler, a memoized Row component shows "Props changed: onSelect" every time its parent's unrelated state updates. The parent defines onSelect as an arrow function in its body. What is the cause, and what is the fix?
Show answer
The arrow function is recreated on every parent render, so onSelect has a new reference and memo sees a changed prop. Wrap the function in useCallback with the right dependencies, or define it outside the component if it uses no component state. Record again to confirm that Row is gray on those commits.