# React.memo

> How to prevent expensive child components from re-rendering unnecessarily when their parent re-renders.

Subject: React | Lesson 30 of 48 | 4 min read
Interactive lesson: https://codewithanimation.in/react/react-memo
Previous: [Complex State with useReducer](https://codewithanimation.in/react/usereducer.md) | Next: [useMemo & useCallback](https://codewithanimation.in/react/usememo-and-usecallback.md)

## The Re-render Cascade

In React, when a component's state changes, it re-renders. By default, **all of its children will also re-render**, regardless of whether their props changed or not.

React assumes that if a parent changed, the children *might* need to change too, and rendering is usually fast enough that checking isn't worth the effort.

`React.memo` is a tool that tells React: 'Hey, this component is heavy. Only re-render it if its props actually change.'

## When rendering is expensive

Imagine a Dashboard that has a simple text input (which updates state on every keystroke) and a massive Data Chart component (which takes 200ms to render).

Because the input is in the parent, typing in it causes the parent to re-render. This forces the heavy Data Chart to re-render on every single keystroke, causing severe lag.

By wrapping the Data Chart in `React.memo`, it will check its props. Since the charting data hasn't changed, it will skip the re-render entirely, keeping the input fast and snappy.

## Wrapping your Component

To use it, you simply wrap your component definition in `memo()`:

`const ExpensiveChart = memo(function ExpensiveChart({ data }) { ... });`

Now, when the parent re-renders, React will do a 'shallow comparison' of the old `data` prop and the new `data` prop. If they are identical (e.g., `oldProps.data === newProps.data`), the component will not re-render.

## Common mistake: The Shallow Equality Trap

`React.memo` only does a *shallow* comparison (`===`).

If you pass an object, array, or function as a prop, they are re-created in memory every time the parent renders. Therefore, `oldProp === newProp` will evaluate to `false`, and `React.memo` will fail to stop the re-render.

To fix this, you must combine `React.memo` with `useMemo` (for objects/arrays) or `useCallback` (for functions).

## Tip: Don't memoize everything!

A common beginner mistake is wrapping *every* component in `React.memo`. Checking props isn't free. For simple components (like a button or a text span), it is actually faster for React to just re-render them than it is to run the prop comparison math.

Only use `React.memo` when a component is visibly slow, or when it has hundreds of descendants.

## Try it: The Render Cascade

Interact with the Dashboard below. Notice how changing the unrelated `count` state forces the regular 'Heavy Component' to flash (re-render), while the 'Memoized Heavy Component' safely bails out.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/react/react-memo

## Check yourself

### Question 1

By default, what happens to a child component when its parent's state changes, but the props passed to the child remain exactly the same?

- A. React automatically skips rendering the child.
- B. The child component re-renders.
- C. React throws a warning about unnecessary re-renders.

Correct answer: B

React's default behavior is to cascade renders down the entire subtree.

- Why not A: React does not do this automatically, because prop checking costs performance.
- Why not C: React considers this normal behavior.

### Question 2

What does `React.memo` actually do?

- A. It caches the result of expensive mathematical calculations.
- B. It prevents a component from re-rendering if its props are shallowly equal to its previous props.
- C. It saves the component's state to localStorage.

Correct answer: B

`React.memo` is a Higher Order Component that acts as a gatekeeper, blocking renders if the props haven't changed.

- Why not A: That is what the `useMemo` hook does, not `React.memo`.
- Why not C: `React.memo` has nothing to do with persistent storage.

### Question 3

If a component is wrapped in `React.memo`, but receives an inline object prop like `<UserCard user={{ name: 'John' }} />`, what will happen?

- A. It will never re-render.
- B. It will re-render every time the parent renders, defeating the purpose of `memo`.
- C. React will throw an error about passing objects to memoized components.

Correct answer: B

Because `{ name: 'John' } === { name: 'John' }` is `false` in JavaScript (different memory addresses), `React.memo`'s shallow comparison fails.

- Why not A: The object changes reference every time.
- Why not C: React won't error, it will just quietly re-render.

## Remember this

- By default, parent renders force all children to render.
- `React.memo` prevents a component from rendering if its props haven't changed.
- It only performs a 'shallow comparison' (using `===`).
- Passing new arrays, objects, or functions as props will break `React.memo`.
- Don't wrap everything in `memo`; only use it for genuinely heavy components.
