Trivia Questions2 min readPremium

useCallback in React

useCallback(fn, deps) returns the same function reference on every render until one of its dependencies changes. It doesn’t make the function faster. It only matters when something else compares that reference: a memoized child, an effect’s dependency array, or a custom hook’s consumer.

1. The problem it solves

Every render creates new function objects. A new reference makes a memoized child think its props changed.

const Row = React.memo(function Row({ item, selected, onSelect }) {
  return (
    <li aria-selected={selected} onClick={() => onSelect(item.id)}>
      {item.name}
    </li>
  );
});

function List({ items }) {
  const [selectedId, setSelectedId] = useState(null);

  // Without useCallback: a new function each render, so every Row re-renders.
  const handleSelect = useCallback((id) => setSelectedId(id), []);

  return (
    <ul>
      {items.map((item) => (
        <Row
          key={item.id}
          item={item}
          selected={item.id === selectedId}
          onSelect={handleSelect}
        />
      ))}
    </ul>
  );
}

With a stable handleSelect, clicking a row re-renders only the two rows whose selected prop changed.

2. The second use: effect dependencies

function Search({ query }) {
  const fetchResults = useCallback(
    (signal) => fetch(`/api/search?q=${encodeURIComponent(query)}`, { signal }),
    [query]
  );

  useEffect(() => {
    const controller = new AbortController();
    fetchResults(controller.signal).catch(() => {});
    return () => controller.abort();
  }, [fetchResults]); // re-runs only when query changes
}

Without useCallback, fetchResults would be new on every render and the effect would run every time.

3. Stale closures

The function captures values from the render it was created in. Leave a value out of deps and you read old data.

// Bug: count is always the value from the first render
const increment = useCallback(() => setCount(count + 1), []);

// Fix 1: list the dependency
const increment = useCallback(() => setCount(count + 1), [count]);

// Fix 2: functional update, no dependency needed
const increment = useCallback(() => setCount((c) => c + 1), []);

Fix 2 is usually better: the reference stays stable forever.

4. useCallback vs useMemo

useCallback(fn, deps) is the same as useMemo(() => fn, deps).

  • useMemo caches the result of calling a function.
  • useCallback caches the function itself.

5. When not to use it

  • The function is only passed to plain DOM elements (<button onClick={...}>). Nothing compares the reference.
  • The child isn’t wrapped in React.memo, so it re-renders with its parent anyway.
  • The dependencies change on almost every render, so the cache never hits.

Each useCallback has a small cost: React stores the function and compares dependencies on every render. Measure with the React Profiler before adding it everywhere.

6. React Compiler

Projects using the React Compiler get this memoization applied automatically at build time, so hand-written useCallback becomes rare there. You still need to understand it: most codebases have plenty of it, and interviewers ask why it exists.

Choosing: do you need useCallback?

Situation Use it?
Passed to a React.memo child or a virtualised list row Yes
Listed in a useEffect / useMemo dependency array Yes
Returned from a custom hook others will depend on Yes
Inline handler on a DOM element No
Child isn’t memoized No

Follow-up questions to expect

  • “Does useCallback prevent the function from being created?” No. The inline function is still created each render; React just hands back the cached one.
  • “Why did my memoized child still re-render?” Another prop changed reference (often an inline object or array), or context changed. See React rendering and re-renders.
  • “How would you avoid the dependency on state?” Functional updates, a reducer, or a ref that holds the latest value.