React Development
Your React Components Re-render Too Often? Find the Cause Before Adding More useMemo
Does your React application feel slow even when the API is fast? Unnecessary component re-renders may be the reason. Learn how to identify rendering bottlenecks, use React DevTools Profiler, manage state correctly, and apply memo, useMemo, and useCallback only when they actually help.
React Development
Your React Components Re-render Too Often? Find the Cause Before Adding More useMemoDoes your React application feel slow even when the API is fast? Unnecessary component re-renders may be the reason. Learn how to identify rendering bottlenecks, improve state management, and use React's performance tools effectively.
The problem: your interface updates more than necessary
Imagine a dashboard with a sidebar, navigation bar, statistics cards, charts, and a large table. A user changes one filter, but several unrelated components appear to update as well.
The application may still work correctly, but unnecessary rendering work can become noticeable as the interface grows. Typing into a search field might feel delayed, dropdowns may become sluggish, and large lists may take longer to update.
React re-renders components for legitimate reasons, such as state changes, new props, or context updates. A re-render is not automatically a performance problem. The real goal is to find expensive work that happens unnecessarily and remove the cause rather than blindly preventing every render.
1. Understand why React components re-render
A component commonly re-renders when:
- Its own state changes.
- Its parent component re-renders and React renders its children as part of the update.
- Its props change.
- A context value it consumes changes.
Consider this simple example:
import { useState } from 'react';
function Dashboard() {
const [count, setCount] = useState(0);
return (
<div>
<h1>Dashboard</h1>
<button onClick={() => setCount(count + 1)}>
Count: {count}
</button>
<UserProfile />
</div>
);
}
function UserProfile() {
console.log('UserProfile rendered');
return <h2>User Profile</h2>;
}
When the count changes, the Dashboard renders again. React also renders the UserProfile child as part of the parent's update because it is not memoized.
This is not necessarily harmful. A small component that returns a heading is inexpensive to render. Optimization becomes important when a component performs expensive calculations, renders a large tree, or updates frequently enough to affect the user experience.
2. Measure performance before optimizing
One of the biggest mistakes in React development is adding optimization hooks before identifying the actual problem. Start by measuring the component that feels slow.
Use React Developer Tools Profiler
Install React Developer Tools in your browser. Open the application, navigate to the Profiler tab, and record the interaction that feels slow.
You can then inspect:
- Which components rendered during the interaction.
- How much time each component took to render.
- Which components rendered again without meaningful prop changes.
- Whether a particular part of the component tree is responsible for the delay.
Use the Profiler API
React also provides a Profiler component for collecting rendering measurements programmatically.
import { Profiler } from 'react';
function onRender(
id,
phase,
actualDuration,
baseDuration
) {
console.log({
id,
phase,
actualDuration,
baseDuration,
});
}
function App() {
return (
<Profiler id="Dashboard" onRender={onRender}>
<Dashboard />
</Profiler>
);
}
The Profiler callback provides measurements when a profiled tree commits an update. Profiling adds overhead and is disabled in ordinary production builds by default, so use it deliberately.
3. Keep state close to where it is needed
A common source of unnecessary rendering is placing frequently changing state too high in the component tree.
Consider a page that contains a dashboard and a search field. If the search input's state lives in the entire dashboard component, each keystroke updates that parent.
You can isolate the search state inside a smaller component:
import { useState } from 'react';
function Dashboard() {
return (
<div>
<h1>Dashboard</h1>
<SearchBox />
<Statistics />
<RecentOrders />
</div>
);
}
function SearchBox() {
const [query, setQuery] = useState('');
return (
<input
value={query}
onChange={event => setQuery(event.target.value)}
placeholder="Search orders..."
/>
);
}
Now typing into SearchBox updates its local state instead of requiring Dashboard itself to own that state. This keeps unrelated state and rendering work better isolated.
4. Use React.memo when a component genuinely benefits
React's memo lets a component skip re-rendering when its props have not changed according to
React's comparison rules.
For example:
import { memo, useState } from 'react';
const UserProfile = memo(function UserProfile({ name }) {
console.log('UserProfile rendered');
return <h2>Welcome, {name}</h2>;
});
function Dashboard() {
const [count, setCount] = useState(0);
return (
<div>
<button onClick={() => setCount(count + 1)}>
Count: {count}
</button>
<UserProfile name="Alex" />
</div>
);
}
The UserProfile component can skip rendering when its name prop remains unchanged, even when Dashboard updates.
However, memoization is a performance optimization, not a guarantee. A memoized component can still render when its own state or consumed context changes. It also cannot help when its props are always new objects or functions.
5. Use useMemo for expensive calculations
Sometimes the component is not slow because of its JSX. It is slow because it performs an expensive calculation every time it renders.
For example, filtering a large collection:
function ProductList({ products, category }) {
const filteredProducts = products.filter(
product => product.category === category
);
return (
<ul>
{filteredProducts.map(product => (
<li key={product.id}>{product.name}</li>
))}
</ul>
);
}
If filtering is measurably expensive and the dependencies do not change on every render, you can cache the result
with useMemo:
import { useMemo } from 'react';
function ProductList({ products, category }) {
const filteredProducts = useMemo(() => {
return products.filter(
product => product.category === category
);
}, [products, category]);
return (
<ul>
{filteredProducts.map(product => (
<li key={product.id}>{product.name}</li>
))}
</ul>
);
}
React reuses the calculated value while the dependencies remain unchanged. Remember that useMemo does not make the initial calculation faster. It can skip recalculating on later renders.
6. Use useCallback when function identity matters
JavaScript creates a new function when a component executes a function expression again. This can matter when passing callbacks to memoized child components.
Consider this example:
import { memo, useState } from 'react';
const SaveButton = memo(function SaveButton({ onSave }) {
console.log('SaveButton rendered');
return <button onClick={onSave}>Save</button>;
});
function Editor() {
const [title, setTitle] = useState('');
const handleSave = () => {
console.log('Saving:', title);
};
return (
<div>
<input
value={title}
onChange={event => setTitle(event.target.value)}
/>
<SaveButton onSave={handleSave} />
</div>
);
}
The callback is recreated whenever Editor renders. Therefore, the memoized SaveButton receives a different function prop and cannot skip rendering based on unchanged props.
You can use useCallback when stable function identity is useful:
import { memo, useCallback, useState } from 'react';
const SaveButton = memo(function SaveButton({ onSave }) {
return <button onClick={onSave}>Save</button>;
});
function Editor() {
const [title, setTitle] = useState('');
const handleSave = useCallback(() => {
console.log('Saving:', title);
}, [title]);
return (
<div>
<input
value={title}
onChange={event => setTitle(event.target.value)}
/>
<SaveButton onSave={handleSave} />
</div>
);
}
The callback stays the same while its dependencies stay the same. When title changes, the callback changes too, which is correct because it uses the latest title.
7. Watch out for objects and arrays passed as props
A frequent reason memoization fails is creating a new object or array during every render.
<UserCard
user={{ name: 'Alex', role: 'Developer' }}
/>
Even if the object contains the same values, it is a new object reference each time the parent renders. A memoized child sees that its prop changed.
One solution is to pass only the values the child actually needs:
<UserCard
name="Alex"
role="Developer"
/>
If the object itself needs to remain stable and there is a measured reason to do so, use
useMemo to preserve its reference. Do not introduce memoization just to avoid creating small objects.
8. Avoid unnecessary Effects that update state
Another common performance problem comes from using Effects to calculate values that can be derived during rendering.
For example, avoid this pattern when the filtered list can be calculated directly:
const [filteredProducts, setFilteredProducts] = useState([]);
useEffect(() => {
setFilteredProducts(
products.filter(product => product.active)
);
}, [products]);
This introduces an additional state update after rendering. If the value is derived entirely from props, calculate it during rendering instead:
const filteredProducts = products.filter(
product => product.active
);
If the calculation is expensive, measure it and consider useMemo. Effects should be used to synchronize with external systems, not as a default way to calculate derived values.
9. Consider React Compiler
Modern React tooling includes React Compiler, which can automatically apply many memoization optimizations when it is configured and supported by your project.
This can reduce the need to manually add memo, useMemo, and useCallback
everywhere. It does not eliminate the need to write pure components, structure state sensibly, or profile
performance problems.
Before adding manual optimizations, check whether React Compiler is enabled in your application and follow the setup guidance for your build environment.
10. A practical React performance checklist
- Use React Developer Tools Profiler to identify expensive renders.
- Keep frequently changing state close to the components that need it.
- Remove unnecessary Effects that create extra state updates.
- Use memo when a component is expensive and often receives unchanged props.
- Use useMemo for calculations that are measurably expensive.
- Use useCallback when stable callback identity helps a memoized child or Hook dependency.
- Check whether objects and arrays are recreated unnecessarily.
- Test performance using a production build and realistic data.
- Do not treat every re-render as a bug.
Final thoughts
React re-renders are a normal part of how the framework updates an interface. The goal is not to eliminate them. The goal is to make sure expensive rendering work happens only when it is actually needed.
Start with the Profiler, improve state placement, remove unnecessary update chains, and apply memoization only where measurements show a real benefit. If your project uses React Compiler, understand what it already optimizes.
Do not make your React code more complicated to prevent renders that were never causing a problem. Measure first, optimize second.