The Basic Syntax: Wrapping Your Component
You'll learn the two ways to wrap a component with React.memo to enable shallow prop comparison.
The Core Idea
Once you've decided a component needs memoization (because you've profiled and confirmed unnecessary re-renders are slowing things down), React provides a simple wrapper function called React.memo(). You literally wrap your component definition with it, like putting a protective shell around it. This tells React: "Before re-rendering this component, check if its props actually changed first."
Two Syntax Options
Option 1: Wrap after definition
function MyComponent({ name, count }) {
return <div>{name}: {count}</div>;
}
export default React.memo(MyComponent);
Option 2: Import memo and wrap inline
import { memo } from 'react';
function MyComponent({ name, count }) {
return <div>{name}: {count}</div>;
}
export default memo(MyComponent);
Both approaches do exactly the same thing—they create a memoized version of your component. The second option is slightly cleaner since you don't need to type React. every time.
Think of It Like a Gatekeeper
Imagine your component is a function that someone keeps calling repeatedly. React.memo puts a gatekeeper at the door who says: "Wait, are the inputs (props) actually different from last time? No? Then I'll just give you the previous result instead of doing all that work again."
The syntax is deliberately simple—you're not changing your component logic at all, just wrapping the final result.
Key Takeaway: Use React.memo(Component) or export default memo(Component) to wrap your component definition and enable automatic shallow prop comparison before re-renders.