Skip to content
tezvyn:

Explain the difference between React.memo and useMemo

Source: react.devEasyHow cards are made

Explain the difference between React.memo and useMemo
Summary

whether you distinguish component-level memo from value caching.

Key points

React.memo skips re-renders when props match; useMemo caches a computed value between renders.

Watch out for

saying they are interchangeable or both stop re-renders.

What's really being asked

This question checks if you know where memoization lives in the React tree. React.memo is a higher order component that lives outside the component it wraps. It memoizes the component itself by comparing previous and next props with Object.is. If they are the same React reuses the last rendered output and skips calling the component function. useMemo is a hook that lives inside a component. It memoizes a value returned from a function between renders of that component. It does not stop the component from rendering. It only avoids repeating an expensive calculation on every render when the dependency array has not changed.

The full answer

First define the scope difference. React.memo operates at the component boundary and is used when a parent re-renders but the child should not. useMemo operates inside a single component and is used when a computation is expensive and its inputs change rarely. Second mention the API shape. React.memo is called as React.memo(MyComponent) or exported as export default memo(MyComponent). useMemo is called inside the component body as const value = useMemo(() => compute(a, b), [a, b]). Third give a clear decision rule. Choose React.memo when you want to avoid rendering a child whose props are stable. Choose useMemo when you want to avoid redoing a heavy calculation or creating a new object reference that would break downstream memoization. Fourth note that React.memo compares props while useMemo compares dependencies.

The mistakes people make

A red flag is claiming that useMemo prevents a component from re-rendering. It does not. It only caches the value returned by the function. Another red flag is saying React.memo caches the result of a calculation inside a component. It does not. It caches the rendered output of an entire component. Some candidates also say you can wrap a component definition with useMemo. That is incorrect because useMemo is a hook and cannot be used at module scope. Also avoid saying either is always needed. Modern React with the React Compiler can automatically memoize in many cases reducing manual useMemo needs.

What usually comes next

The interviewer may ask how React.memo compares props. You should say shallow comparison by default or custom comparison function as a second argument. They may ask when useMemo is actually worth the cost. You should say when the calculation is measurably expensive usually more than a few milliseconds or when the returned object identity matters for a downstream useEffect or child memo. They may also ask about useCallback versus useMemo. You should explain that useCallback is a specialization of useMemo for functions.

A concrete example

Imagine a chart dashboard. The parent component holds the selected date range and re-renders whenever the user clicks a preset. The heavy Chart component receives data and color props. If the data and color props are unchanged you wrap Chart in React.memo so the parent re-render does not force Chart to re-render. Inside Chart you transform ten thousand raw data points into a mapped SVG path array. You wrap that mapping in useMemo with the raw data as a dependency so the path array is only rebuilt when the data changes not on every parent re-render.

Interview question

Which approach correctly prevents a child component from re-rendering when its props are unchanged?

  • a.Wrap the child component with React.memo to let React reuse its previous output when props matchCorrect
  • b.Use useMemo at the module scope to wrap the component definition and cache its rendered output
  • c.Use useMemo inside the child to cache an expensive calculation and prevent the component from re-rendering
  • d.Call useMemo inside the child to return its JSX and skip rendering when props are stable
Why?

React.memo is a higher-order component that compares previous and next props with Object.is and reuses the last rendered output, skipping the component call entirely. The most tempting distractor suggests using useMemo to block rendering, but useMemo is a hook that only caches a computed value inside a component and does not prevent that component from executing.

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