React Re-Renders: Why They Happen & How to Fix Expensive Ones
In this article
React components re-render when their state, props, or context changes, and expensive re-renders happen when large component trees or heavy computations run unnecessarily. You fix them by memoizing components and values, stabilizing function references, and splitting state so only the parts that actually changed re-render.
What a Re-Render Actually Is
In React, a re-render is the process of calling a component function again to produce a new React element tree. React then compares the new tree with the previous one (a process called reconciliation) and updates only the parts of the real DOM that differ. This is efficient by design, but the comparison itself costs time, and if a component renders thousands of elements or performs expensive calculations, every re-render adds up.
It's important to distinguish re-rendering from mounting and unmounting. Mounting is when a component is created for the first time; unmounting is when it's removed. Re-rendering is simply updating an existing component. A component can re-render many times without being unmounted, and each re-render runs the entire function body.
Why Re-Renders Happen: The Three Triggers
React re-renders a component for exactly three reasons:
- State changes: When you call
setState(oruseState's updater), the component that owns that state re-renders. This is the most common trigger. - Props changes: When a parent component re-renders, it passes new props to its children. Even if the prop values are identical (by reference), a new object or array is created, so the child sees a different reference and re-renders.
- Context changes: When the value provided by a
Context.Providerchanges, every component that consumes that context re-renders, regardless of whether they use the specific part that changed.
Additionally, a parent re-render will re-render all its children by default, unless those children are memoized. This cascading behavior is why a single state update at the top of a large tree can cause dozens of components to re-render.
How to Identify Expensive Re-Renders
Before optimizing, you need to know which re-renders are actually costly. React's built-in Profiler (in React DevTools) records render durations and shows you a flamegraph of which components took the longest. Look for components that render slowly or render frequently, especially ones deep in the tree that don't depend on the changed state.
Common signs of expensive re-renders:
- A large list or table re-renders when you type in an input field.
- Animations or drag-and-drop feel janky because heavy components re-render on every frame.
- A component that renders a chart or a complex SVG re-renders when unrelated state changes.
You can also add a temporary console.log in a component body to see how often it renders. But the Profiler is more precise—it shows both the cause (which state update triggered the render) and the duration.
Fixing Expensive Re-Renders: Memoization and Beyond
Once you've identified the culprit, the main tools are:
React.memo: Wraps a component to skip re-rendering if its props are shallowly equal to the previous props. Use it for components that receive stable props and render often.useMemo: Caches the result of a computation and only recomputes when its dependencies change. Use it for expensive calculations (e.g., filtering a large array) that shouldn't run on every render.useCallback: Returns a memoized function reference, so child components that rely on reference equality (like those wrapped inReact.memo) don't re-render when the parent re-renders.- State colocation: Move state down to the component that actually uses it. If only one part of a form needs to update, keep that state in that component, not in a shared parent.
- Context splitting: If you use Context, split it into multiple providers so that a change in one value doesn't re-render all consumers of the whole context.
For example, if you have a TodoList component that receives a todos array and an onToggle function, wrap it in React.memo and wrap onToggle in useCallback in the parent. Then the list only re-renders when the todos array actually changes, not when the parent re-renders for other reasons.
Common Mistakes and Misconceptions
A frequent mistake is overusing React.memo on every component. Memoization has a cost: React must compare props on every render, and the comparison itself takes time. For small, simple components, that overhead can be higher than the cost of just re-rendering. Only memoize components that are large, render often, or receive complex props.
Another misconception is that useMemo and useCallback guarantee that a computation won't run. They only help if the dependencies actually stay the same. If you pass an inline object as a dependency, it changes every render, and the memo is useless. Always ensure dependencies are stable (primitives or memoized values).
Also, remember that React.memo does a shallow comparison by default. If you pass a prop that is a new object or array every render, the memo will never skip re-rendering. You'd need a custom comparator or to memoize the prop itself.
Alternatives to Memoization
Sometimes restructuring your component tree is more effective than memoizing. Consider:
- Lifting state up is often necessary, but you can also lift content down: pass elements as props instead of letting a component render them. For example, instead of a
Modalcomponent that renders a heavy chart, pass{chart}as a prop so the chart doesn't re-render when the modal's state changes. - Using
useReducerfor complex state logic can reduce the number of state updates, but it doesn't automatically reduce re-renders—it just organizes updates. - External state libraries like Zustand or Redux with selectors can limit re-renders to components that select a specific slice of state, but they add complexity.
- React 18's
useTransitioncan mark non-urgent updates (like filtering a large list) as transitions, so they don't block the UI and can be interrupted.
The best approach is to measure first, then apply the simplest fix that works. Often, moving state down or splitting context is enough, and you don't need to memoize anything.
Frequently Asked Questions
Does React.memo prevent re-renders caused by context changes?
No. React.memo only skips re-renders when props are unchanged. If a component consumes a context and that context value changes, it will re-render regardless of memoization. To avoid that, you need to split the context or use a selector-based library.
Why does my component re-render when I type in an input?
If the input's value is controlled by state in a parent, every keystroke updates that state, causing the parent and all its children to re-render. To fix it, either move the state into the input's own component, or memoize the heavy children so they don't re-render when the input's state changes.
Is it bad to use useMemo for everything?
Yes. useMemo has overhead and makes code harder to read. Use it only for computations that are genuinely expensive (e.g., filtering thousands of items) or for values that are used as dependencies in other hooks. For simple calculations, just compute them inline.
What's the difference between useMemo and useCallback?
useMemo returns the result of a function and caches it. useCallback returns the function itself, cached. They are related—useCallback(fn, deps) is essentially useMemo(() => fn, deps)—but you use useCallback when you want to pass a stable function reference, and useMemo when you want to avoid recomputing a value.
Can I force a re-render in React?
Yes, you can call setState with a new object (e.g., setState({})) or use the useReducer dispatch with a new action. But forcing re-renders is rarely necessary and often a sign of a design issue. Prefer to structure state so that re-renders happen naturally when data changes.