Custom Comparator Use Case: Ignoring Specific Props
What you'll learn: How to use arePropsEqual to skip re-renders by comparing only the props that matter to your component.
The Problem: Props You Don't Actually Care About
Sometimes a parent passes down props your component doesn't use for rendering—maybe a callback that changes reference, or metadata you log but don't display. React.memo's default shallow comparison will trigger re-renders whenever any prop changes. But what if you want to ignore certain props entirely?
The Solution: Custom Comparison Logic
The arePropsEqual function you provide to React.memo lets you write custom comparison logic. You can choose which props to check and which to ignore.
const MyComponent = React.memo(({ userId, userName, onDebug }) => {
// onDebug changes every render but we don't care
return <div>User: {userName}</div>;
}, (prevProps, nextProps) => {
// Return true if props are "equal enough" (skip re-render)
// Only compare userId and userName, ignore onDebug
return prevProps.userId === nextProps.userId &&
prevProps.userName === nextProps.userName;
});
How It Works
Your comparator receives the previous and next props. If you return true, React skips the re-render (props are considered equal). If you return false, React re-renders.
Think of it like a bouncer at a club: "I only check IDs and names. Don't care what's in your bag."
Common Scenarios
- Ignoring callback props that change reference but don't affect rendering
- Ignoring timestamp or debug metadata
- Ignoring props used only in event handlers, not in JSX
Key Takeaway: Use a custom arePropsEqual function to tell React.memo exactly which props matter for re-rendering, letting you ignore props that don't affect your component's output.