Memoizing Components with React memo
Memoizing Components with React memo
1. What is React.memo?
By default, when a parent component re-renders, all of its children re-render. If a child component is computationally heavy or renders a large list, re-rendering it unnecessarily when its props have not changed wastes CPU cycles.
React.memo is a Higher-Order Component that memoizes a functional component.
When a component is wrapped in React.memo, React skips rendering the component if its props have not changed since the last render.
$$\text{Next Props} \stackrel{\text{shallow equal}}{===} \text{Prev Props} \implies \text{Skip Render (Reuse Cached VDOM)}$$
2. Basic Usage of React.memo
If the parent component re-renders (due to an unrelated state change like a clock ticker), CourseSummaryCard will skip rendering entirely, reusing its previous Virtual DOM result.
3. The Shallow Comparison Trap
By default, React.memo compares props using shallow equality (Object.is):
- Primitives (strings, numbers, booleans) are compared by value (
'React' === 'React' -> true). - Complex types (objects, arrays, functions) are compared by memory reference (
{} === {} -> false)!
If a parent passes an unmemoized inline object or function to a memoized child, React.memo will completely fail to stop re-renders!
To fix this, objects must be memoized with useMemo, and functions must be memoized with useCallback.
4. Providing a Custom Comparison Function
If you need deep or selective comparison, React.memo accepts a custom comparison function as its second argument:
(Notice: Return true to SKIP render; return false to RE-RENDER. This is the opposite of shouldComponentUpdate!)
5. When NOT to Use React.memo
Do not wrap every component in React.memo!
- Memoization has a cost: React must run equality checks on every prop before every render.
- For lightweight components (a simple button or icon), the prop comparison check is often more expensive than just letting the component re-render!
- Use
React.memoonly on heavy components, large list items, or components with expensive rendering trees.
Practice Quiz
React.memo do when applied to a functional component?onClick={() => doSomething()}) break React.memo on a child component?React.memo(Component, arePropsEqual) return if props are identical and rendering should be skipped?React.memo genuinely beneficial?React.memo prevent it from re-rendering if its OWN internal useState updates?The useCallback Hook and Stabilizing Callback Props
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| React Re-render Rules and Profiling Bottlenecks | The useCallback Hook and Stabilizing Callback Props |
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.