Sub topic 7
Common causes of unnecessary re-renders
This chapter assumes you can write function components with props, state, and hooks, and that you have recorded a Profiler session and can find the "Why did this render?" information. Here you will learn the usual reasons a component renders when its output does not change.
What makes a render unnecessary
A render is unnecessary when React calls your component and the result is the same as last time. The render costs time, but no DOM change follows. One wasted render is cheap. The problem appears when many of them add up, or when one of them is a heavy component.
React renders a component for one of these reasons:
- Its own state changed.
- Its parent rendered.
- A context it reads changed.
- A hook it uses changed, such as a store subscription.
Most wasted renders come from the second reason.
Cause 1: the parent rendered
By default, when a component renders, React renders all of its children. It does this even when their props are identical. Typing in an input that holds state at the top of the page re-renders everything below it.
In the Profiler, this shows up as a component with the reason "The parent component rendered" and no changed props.
Cause 2: new object, array, and function identities
Props are compared by reference. Every render creates new objects, arrays, and functions, so they look "changed" even when their contents are the same.
import { memo } from "react";
const Chart = memo(function Chart({ options, onZoom }) {
return <canvas />;
});
function Panel() {
// Both values are new on every render of Panel,
// so memo(Chart) can never skip rendering.
return <Chart options={{ smooth: true }} onZoom={() => {}} />;
}memo compares props with Object.is. A fresh { smooth: true } is never equal to the previous one. This is the most common reason that memoization "does not work".
Cause 3: context value changes
Every component that calls useContext re-renders when the provider's value changes. A provider that builds its value inline creates a new value on every render:
import { createContext, useState } from "react";
export const ThemeContext = createContext(null);
export function ThemeProvider({ children }) {
const [theme, setTheme] = useState("light");
// New object each render: all consumers re-render
// whenever ThemeProvider renders, even if theme is unchanged.
return (
<ThemeContext.Provider value={{ theme, setTheme }}>
{children}
</ThemeContext.Provider>
);
}A related problem is a single context that holds unrelated data. A consumer that needs only theme still re-renders when other fields change. memo does not help here, because context bypasses props.
Cause 4: state held too high
State kept in a shared ancestor forces everything under it to render on each update. This is the same mechanism as cause 1. The fix is structural, and the next chapter covers it.
Cause 5: unstable keys and component definitions
Two mistakes force React to throw away and rebuild subtrees, which is worse than a re-render:
- Using
key={Math.random()}or an array index on a reorderable list. A changed key unmounts and remounts the component, and it loses its state. - Defining a component inside another component. It is a new function type on each render, so React remounts it every time.
function Page() {
// Wrong: Inner is a new component type on every render of Page.
function Inner() {
return <input />;
}
return <Inner />;
}Move Inner outside Page and pass data through props.
Matching a symptom to a cause
| Profiler reason | Likely cause |
|---|---|
| The parent component rendered | Cause 1 or 4 |
| Props changed: `options`, `onClick` | Cause 2 |
| Context changed | Cause 3 |
| Component mounts on every update | Cause 5 |
Exercise
A component wrapped in memo still re-renders every time its parent renders. The parent passes style={{ color: "red" }}. Why does memo not skip the render, and what are two ways to fix it?
Show answer
The style object is created fresh on every parent render, so the shallow prop comparison sees a different reference. Fix it by moving the object to a constant outside the component, or by wrapping it in useMemo with an empty dependency array.