๐ React.memo
for Component Memoization โ Optimize React Renders (2025 Guide)
๐งฒ Introduction โ Why Use React.memo
?
React.js re-renders components by default whenever a parent re-rendersโeven if the childโs props havenโt changed. This behavior is often unnecessary and can slow down your app.
React.memo
helps prevent unwanted re-renders by memoizing functional components. If the props stay the same, React skips rendering that component entirely.
๐ฏ In this guide, youโll learn:
- What
React.memo
does and how it works - Real-world usage examples
- How to combine it with
useCallback
anduseMemo
- Best practices and common pitfalls
๐ง What is React.memo
?
React.memo
is a higher-order component that wraps a functional component and memoizes it based on its props.
โ Syntax:
const MemoizedComponent = React.memo(MyComponent);
๐ Works like PureComponent
for class components
๐ Skips rendering unless props change
๐ฆ 1. Basic Example
const Message = React.memo(({ text }) => {
console.log('Rendered:', text);
return <p>{text}</p>;
});
โ Used in a parent:
function Parent() {
const [count, setCount] = useState(0);
return (
<>
<Message text="Hello!" />
<button onClick={() => setCount(count + 1)}>Increment</button>
</>
);
}
โ
Message
doesn’t re-render when count
updates
โ
Saves unnecessary renders
โ 2. Custom Comparison Function
By default, React.memo
does a shallow comparison of props. For complex or nested objects, you can supply a custom function:
const areEqual = (prevProps, nextProps) => {
return prevProps.user.id === nextProps.user.id;
};
const UserProfile = React.memo(({ user }) => {
return <h3>{user.name}</h3>;
}, areEqual);
โ Prevents re-renders if deeply nested values don’t change
๐ 3. Combine with useCallback
for Props
Passing inline functions as props will break memoization unless you memoize them with useCallback
.
โ Bad:
<Child onClick={() => console.log('clicked')} />
โ Good:
const handleClick = useCallback(() => {
console.log('clicked');
}, []);
<Child onClick={handleClick} />
๐ React.memo
+ useCallback
= ๐ฅ optimized child rendering
๐งฎ 4. When NOT to Use React.memo
Situation | Reason |
---|---|
Component renders infrequently | Memoization overhead may not be worth it |
Props change on every render | Memoization wonโt help |
Component is very simple (1โ2 lines) | Not worth complexity |
๐ 5. Debug Re-Renders with why-did-you-render
Install this development-only library to inspect memo effectiveness:
npm install @welldone-software/why-did-you-render
โ Usage:
import React from 'react';
if (process.env.NODE_ENV === 'development') {
const whyDidYouRender = require('@welldone-software/why-did-you-render');
whyDidYouRender(React, { trackAllPureComponents: true });
}
โ
Tells you why a component rendered and whether React.memo
helped
๐ Best Practices
โ
Use React.memo
on:
- Functional components with stable props
- List items, avatars, icons, layout components
โ
Always memoize callback props with useCallback
โ
Avoid inline objects or functions as props
โ
Use React.memo
selectivelyโprofile first
๐ Summary โ Recap & Next Steps
React.memo
is a powerful tool to prevent unnecessary re-renders and optimize performance. It works best when components:
- Receive stable props
- Are pure (no internal state or side effects)
- Are frequently re-rendered due to parent updates
๐ Key Takeaways:
- Wrap functional components with
React.memo()
to skip renders - Use
useCallback
to memoize function props - Use a custom comparison function for deep props
- Profile before optimizingโdon’t overuse memoization
โ๏ธ Real-World Relevance:
Used in production React apps to optimize component-heavy UIs, chat systems, forms, dashboards, and data tables.
โ FAQ Section
โ What does React.memo
do?
โ
It memoizes a componentโs render output and prevents re-rendering when props haven’t changed.
โ Does React.memo
work for class components?
โ No. For class components, use PureComponent
.
โ Can I memoize a component with children?
โ
Yes, but memoization only works on props, not internal state or side effects.
โ What breaks memoization in React.memo?
โ
Passing inline objects/functions or props that change identity on each render.
โ Is React.memo always better?
โ No. If the component renders infrequently or props change often, React.memo
may hurt performance due to extra comparisons.
Share Now :