Skip to content
tezvyn:

useCallback: Cache Functions Between Renders

Source: react.devMediumHow cards are made

useCallback: Cache Functions Between Renders

useCallback caches a function so it's not recreated on every render. This is crucial for performance when passing callbacks as props to memoized components, preventing them from re-rendering unnecessarily.

Why it exists

In JavaScript, functions are objects. A function defined inside a React component is recreated as a new object on every single render. This new reference breaks reference equality checks, defeating optimizations like React.memo that rely on stable props to prevent re-renders.

The mental model

Think of useCallback as giving your function a stable memory address. Without it, your component creates a new, identical-looking function at a new address every time it renders. For a child component receiving this function as a prop, it looks like a brand new prop every time, forcing it to re-render even if nothing has visually changed.

How it works

You wrap your function definition in useCallback and provide a dependency array. On the initial render, React returns your function and caches it. On subsequent renders, React compares each item in the dependency array with its previous value. If no dependencies have changed, React returns the cached function from the previous render. If any dependency has changed, it returns the newly created function and caches that one for future renders.

When to use it

The primary use case is passing a callback to an expensive child component that is wrapped in React.memo. This ensures the child component only re-renders when its props actually change, not just because the parent rendered. It's also essential when a function is a dependency of another hook like useEffect, preventing the effect from running on every render.

When not to use it

Do not wrap every function in useCallback. It adds a small amount of complexity and overhead. If a function is not passed to a memoized component or used in a hook's dependency array, using useCallback is often a premature optimization. Note that the upcoming React Compiler aims to automate this, reducing the need for manual memoization.

One canonical example

A ProductPage component passes a handleSubmit function to a memoized ShippingForm. The function is defined as: const handleSubmit = useCallback((details) => { post('/buy', { productId, details }); }, [productId]);. Without useCallback, ShippingForm would re-render every time ProductPage does, because handleSubmit would be a new function object. With useCallback, it only gets a new function if productId changes.

Interview question

A child component wrapped in React.memo receives a function as a prop from its parent. If the parent re-renders, why might the child component also re-render unnecessarily?

  • a.The child component's internal state changed, overriding React.memo's optimization.
  • b.React.memo is only effective for primitive data types, not for function props.
  • c.The parent component did not explicitly declare the function prop as immutable.
  • d.The function prop is a new object reference on each parent render, failing React.memo's equality check.Correct
Why?

Functions defined inside a React component are recreated as new objects on every render. This new reference breaks React.memo's reference equality check for props, causing the child to re-render. Option B is incorrect because React.memo does work with function props by checking their reference equality.

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