Skip to content
tezvyn:

Explain the difference between useMemo and useCallback

Source: react.devMediumHow cards are made

Explain the difference between useMemo and useCallback

This tests whether useMemo caches values while useCallback caches functions, and why that matters for React.memo. A strong answer notes React.memo uses Object.is, so a new inline function prop triggers a child re-render. Red flag: claiming they are the same.

What's really being asked

The interviewer wants to know if you understand referential identity in JavaScript and how React.memo performs shallow prop comparison. They are checking whether you know that useMemo caches a computed value while useCallback caches a function reference, and critically, whether you can explain why a stable function reference is necessary to let a memoized child bail out of rendering.

The full answer

First, define the distinction in one sentence: useMemo takes a function that returns a value and caches that returned value, whereas useCallback takes a function and caches the function itself. Second, explain the React.memo contract: when a component is wrapped in React.memo, React skips rendering if every prop is the same reference as the previous render using Object.is. Third, connect the dots: if a parent creates a new inline function during render and passes it as a prop, React.memo sees a different reference and re-renders the child even if the function body is identical. Fourth, mention that useCallback solves this by returning the same function instance across renders when dependencies are unchanged. Fifth, note that useCallback is unnecessary if the child is not memoized or if the function is not passed as a prop.

The mistakes people make

Saying that useMemo and useCallback do the same thing is a major red flag. Another mistake is claiming that useCallback prevents the parent from re-rendering; it does not, it only stabilizes the reference given to children. Some candidates also say useCallback is only for useEffect dependency arrays, which misses the primary React.memo use case. Over-memoizing every function without profiling is also a red flag for senior candidates because it shows cargo-cult optimization.

What usually comes next

The interviewer may ask when React Compiler removes the need for manual useCallback. They might ask how useCallback interacts with custom Hooks that return stable references. Another follow-up is asking what happens if the dependency array is wrong or empty, or how this pattern scales with a list of memoized items where each row needs its own stable callback.

A concrete example

Imagine a Parent component with a count state and a Child component wrapped in React.memo that receives an onClick handler. If Parent renders and defines handleClick as a regular inline function, JavaScript creates a new function object every time. React.memo compares the old onClick prop with the new one via Object.is, sees they are different, and re-renders Child. If Parent instead uses useCallback with an empty dependency array around the setter, React returns the identical function on subsequent renders. React.memo then sees an unchanged onClick prop and skips the child render entirely, saving layout and paint work.

Interview question

A parent computes an expensive object and defines an inline handler passed to a React.memo child. Which hook combination correctly optimizes the computation and prevents unnecessary child re-renders?

  • a.useMemo for the object and no hook for the handler
  • b.useCallback for both the object and the handler
  • c.useMemo for both the object and the handler
  • d.useMemo for the object and useCallback for the handlerCorrect
Why?

useMemo caches the computed object value, while useCallback preserves the function reference so React.memo's Object.is check sees an unchanged prop and skips the child render. Choosing useMemo for the handler confuses the two hooks because useMemo caches the returned value rather than the function reference, and omitting a hook creates a new inline function on every parent render.

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