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)}$$
React.memoIf 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.
By default, React.memo compares props using shallow equality (Object.is):
'React' === 'React' -> true).{} === {} -> 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.
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!)
React.memoDo not wrap every component in React.memo!
React.memo only on heavy components, large list items, or components with expensive rendering trees.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?Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| React Re-render Rules and Profiling Bottlenecks | The useCallback Hook and Stabilizing Callback Props |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.
Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.