Sub topic 6
Finding out why a component rendered
This chapter assumes you can read a React DevTools Profiler recording, including the flamegraph and ranked charts from the previous chapters. It also assumes you know the difference between a render and a commit, and that you are comfortable with props, state, context, and hooks.
Knowing that a component is slow is half the diagnosis. The other half is knowing why it ran at all. A component that renders for a good reason needs a cheaper render. A component that renders for no good reason needs to be stopped from rendering. The Profiler can tell you which case you have.
Turn on the render reasons setting
The Profiler does not record render reasons by default, because collecting them adds overhead. Enable the setting before you record:
- Open the browser DevTools and select the Profiler tab from React DevTools.
- Click the gear icon, then open the Profiler section.
- Check Record why each component rendered while profiling.
- Record your interaction as usual.
Where the reason appears
After you stop recording, pick a commit in the commit bar at the top of the Profiler. Then click a component in the flamegraph or ranked chart. The right-hand sidebar shows a Why did this render? list for that component in that commit. Hovering over a bar also shows the reason in the tooltip.
Component colors also help you before you read any text. In the flamegraph, a gray bar means the component did not render in the selected commit. Colored bars did render. Selecting a different commit changes which bars are gray, so step through the commits and watch which components stay colored.
The reasons you will see are:
| Reason shown | Meaning |
|---|---|
| This is the first time the component rendered | A mount. Nothing is wrong. |
| Props changed: `a`, `b` | One or more props are not identical (`Object.is`) to the previous render. The names are listed. |
| State changed | The component's own `useState` or `useReducer` state changed. |
| Hooks changed | A hook inside the component changed, such as state, a reducer, or a subscription-based hook. |
| Context changed | A context the component reads has a new value. |
| The parent component rendered | None of the above changed. The component re-rendered only because its parent did. |
The last reason matters most for performance work. It is the signature of a render that did no useful work.
How to read each reason
Props changed
The list of prop names is the useful part. If a component shows "Props changed: onSelect", compare what the parent passes. Props are compared by identity, not by content. These values get a new identity on every parent render:
- Inline objects and arrays, such as
style={{ color: "red" }}oroptions={[1, 2]}. - Inline functions, such as
onClick={() => save(id)}. - The result of
.map(),.filter(), or{ ...obj }computed in the render body.
The component may look unchanged on screen, but the prop is a different object each time. Wrapping the child in React.memo does not help while the prop identity changes. You must stabilize the value first, with useCallback or useMemo, or by moving it outside the component.
State changed and Hooks changed
These reasons mean the component itself asked to render. That is usually legitimate. Ask whether the state belongs in that component. If the state is used only by a small part of the output, moving it into a smaller component limits how much re-renders.
"Hooks changed" can also come from a custom hook that wraps state or a subscription. If several hooks are involved, the reason does not tell you which one fired. You then need to look inside the custom hook, or log values yourself as shown later.
Context changed
Every component that reads a context re-renders when the provider's value changes, even if it uses only one field of that value. A common bug is a provider that builds its value inline:
import { createContext, useState } from "react";
export const SettingsContext = createContext(null);
export function SettingsProvider({ children }) {
const [theme, setTheme] = useState("light");
// A new object on every render of SettingsProvider.
return (
<SettingsContext.Provider value={{ theme, setTheme }}>
{children}
</SettingsContext.Provider>
);
}Whenever SettingsProvider re-renders for any reason, every consumer shows "Context changed". Memoizing the value with useMemo keeps the identity stable until theme changes.
The parent component rendered
This is a re-render with no changed inputs. React renders children by default whenever a parent renders, and it only skips the child if the child is wrapped in memo and its props are shallowly equal. Seeing this reason on an expensive component is your signal to act, in this order:
- Move state down so the parent stops rendering as often.
- Pass the expensive part as
childrenso its element identity does not change. - Wrap the component in
memoif the first two do not apply.
A decision path
Checking by hand with a logging hook
The Profiler is the first tool to reach for. Sometimes it names a prop but you still cannot see how the value differs, or you want a check that runs in the console while you click around. A small development-only hook can log which props changed:
import { useEffect, useRef } from "react";
export function useWhyDidYouUpdate(name, props) {
const previous = useRef(props);
useEffect(() => {
const changes = {};
for (const key of Object.keys({ ...previous.current, ...props })) {
if (!Object.is(previous.current[key], props[key])) {
changes[key] = { from: previous.current[key], to: props[key] };
}
}
if (Object.keys(changes).length > 0) {
console.log(`[${name}] changed props:`, changes);
}
previous.current = props;
});
}
export function Row(props) {
useWhyDidYouUpdate("Row", props);
return <li>{props.item.name}</li>;
}The hook has no dependency array, so it runs after every render and compares each prop with Object.is. If the log shows a prop whose from and to look the same when printed, the identity changed but the content did not. That is an unstable prop. Remove the hook when you finish. It adds work to every render.
Gotchas
- Strict Mode double renders. In development, Strict Mode renders components twice to surface impure code. The Profiler groups these into one commit, but console logs from your render body appear twice. Do not count the doubled logs as extra renders in production.
- Reasons describe one commit. A component can show "State changed" in one commit and "The parent component rendered" in the next. Check several commits before deciding on a fix.
- A render is not always a problem. A cheap component that renders a lot costs almost nothing. Use the render time from the flamegraph to decide whether the reason is worth fixing.
- Memoized components that skipped. A
memocomponent that was skipped appears gray. If it is colored and says "Props changed", memoization is not working. Look for the unstable prop. - Development timings. Render reasons are accurate in a development build, but the durations are inflated. Confirm timings in a production build with profiling enabled.
Worked example
Suppose a list screen is slow while you type in a search box. You record while typing three characters and see three commits. In each commit, 200 Row components are colored, and each reports "Props changed: onSelect".
The parent defines onSelect inline, so it gets a new function on each keystroke. Row is already wrapped in memo, but the new function identity defeats it. The fix is to stabilize onSelect with useCallback. You record again with the same typing and see that in each commit the Row bars are gray. Only the input's component is colored.
The reason list told you which prop to fix. You did not need to guess between the item prop and onSelect.
Exercise
A memoized Row component shows "Props changed: style" in every commit, although the styles on screen never change. The parent passes style={{ padding: 8 }}. What is the cause, and what are two ways to fix it?
Show answer
The inline object literal is a new object on every parent render, so Object.is reports a changed prop and memo cannot skip the render. Fix it by hoisting the object into a constant outside the component (const rowStyle = { padding: 8 };), or by creating it with useMemo(() => ({ padding: 8 }), []) inside the parent. Then record again to confirm that Row is gray in the next commits.
Exercise
You enabled "Record why each component rendered while profiling" after you recorded a session, and the sidebar shows no reasons. What should you do?
Show answer
Record the interaction again. The setting only affects recordings made after it is enabled, so the earlier recording cannot be filled in.