Skip to content
tezvyn:

React.memo: Skip Unnecessary Component Renders

Source: react.devEasyHow cards are made

React.memo: Skip Unnecessary Component Renders

React.memo tells a component to skip re-rendering if its props are the same as last time. Use it to prevent costly renders when a parent updates. The footgun is passing new objects or functions as props, which breaks the default shallow comparison.

Why it exists

By default, React re-renders a component whenever its parent re-renders. This cascading update can be inefficient if the child component is expensive to render and its props haven't actually changed. React.memo provides a way to opt-out of these unnecessary renders.

The mental model

Think of React.memo as a gatekeeper for your component's render function. It holds onto the last set of props it received. When a parent triggers a re-render, the gatekeeper compares the incoming props to the saved ones. If they are identical, it blocks the render and tells React to reuse the previous result.

How it works

You wrap your component in memo, like const MemoizedComponent = memo(MyComponent). This returns a new component. When this new component is about to re-render, React first performs a shallow comparison of its new and old props using Object.is. If all props are equal, React skips re-rendering the component and its children. You can provide an optional custom comparison function as a second argument, but this is rarely needed.

When to use it

Use memo for pure components that render the same output for the same props, especially if they are rendered often or are computationally expensive. It's a performance optimization for situations where a parent's state updates frequently but doesn't affect all of its children.

When not to use it

Don't wrap every component in memo. The prop comparison has a small cost, so if a component's props change on almost every render, memo adds overhead for no benefit. It's also unnecessary for very simple components that are cheap to render. Note that the React Compiler aims to automate this process, potentially reducing the need for manual memo usage.

One canonical example

Imagine a UserProfile component that receives a user object. Its parent component re-renders every second due to a timer. If you wrap it (memo(UserProfile)) but the parent creates a new user object on every render (const newUser = {...}), the memo check fails because the object reference is new. The component will re-render despite its data being the same. To make memo work, the parent must pass the exact same object reference between renders.

Interview question

What is a common reason React.memo might fail to prevent unnecessary re-renders, even when applied to a component?

  • a.The component's internal state changes frequently, triggering its own re-renders.
  • b.The parent component passes new object or function references as props during each re-render.Correct
  • c.The component is too simple and inexpensive to render, making the memoization overhead counterproductive.
  • d.The component uses useContext to consume values from a context provider.
Why?

React.memo performs a shallow comparison of props. If new object or function references are passed as props on each render, even if their content is identical, the shallow comparison will fail, causing the component to re-render. Option A is incorrect because React.memo only optimizes based on prop changes, not internal state changes.

Just read this? Test yourself on what you have been reading.

Read the original → react.dev

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on react — each one lists the topics its interview covers.

See open roles