Skip to content
HooksChapter 20 / 37Advanced4 min read

useMemo & useCallback

Learn how useMemo and useCallback cache expensive calculations and functions between renders, and when you still need them with React Compiler.

On this page

1. What are useMemo and useCallback?

On every re-render, the whole component function runs again. That means every calculation is repeated and every function created inside is a new function.

These two hooks tell React: "unless the dependencies change, give me the previous result."

2. Why do they matter?

Without memoizationWith useMemo / useCallback
Filter 10,000 items on every renderFilter only when the list or query changes
A new function on every renderThe same function while deps stay the same
A memo child still re-rendersThe child's props are the same, so the re-render is skipped
An effect runs on every renderThe effect runs only on real changes

3. Where are they used?

SituationHook
Filtering or sorting a big listuseMemo
Passing a function prop to a memo childuseCallback
An object or function in an effect's dependenciesuseMemo / useCallback
Keeping a Context value object stableuseMemo

4. How do they work?

The memoization flow
The component renders
React compares the dependencies with the previous render
If they are the same, it returns the cached value or function
If they changed, it calculates a new one and caches it

The comparison uses Object.is. So if a dependency is itself a new object on every render, the cache will never work.

useCallback(fn, deps) is really a shortcut for useMemo(() => fn, deps).

5. Code example

useMemo: expensive filtering

tsx
import { useMemo, useState } from "react";

type Product = { id: number; name: string };

export function ProductSearch({ products }: { products: Product[] }) {
  const [query, setQuery] = useState("");
  const [dark, setDark] = useState(false);

  const visible = useMemo(
    () => products.filter((p) => p.name.toLowerCase().includes(query.toLowerCase())),
    [products, query]
  );

  return (
    <div className={dark ? "dark" : ""}>
      <button onClick={() => setDark(!dark)}>Theme</button>
      <input value={query} onChange={(e) => setQuery(e.target.value)} />
      <p>{visible.length} products found</p>
    </div>
  );
}

When you toggle the theme, the component re-renders, but the filtering does not run again because products and query are the same.

useCallback: with a memo child

tsx
import { memo, useCallback, useState } from "react";

const SaveButton = memo(function SaveButton({ onSave }: { onSave: () => void }) {
  return <button onClick={onSave}>Save</button>;
});

export function Editor() {
  const [text, setText] = useState("");
  const [count, setCount] = useState(0);

  const handleSave = useCallback(() => {
    console.log("Saving", text);
  }, [text]);

  return (
    <>
      <textarea value={text} onChange={(e) => setText(e.target.value)} />
      <button onClick={() => setCount(count + 1)}>Clicks: {count}</button>
      <SaveButton onSave={handleSave} />
    </>
  );
}

How does the code work?

PartMeaning
useMemo(() => ..., [products, query])The filter result stays cached while the deps stay the same
memo(SaveButton)The child skips re-rendering when its props are the same
useCallback(..., [text])handleSave stays the same when count changes
ResultSaveButton does not re-render when the click count goes up

6. Common mistakes

Mistake: Wrapping every small thing in useMemo

Why? Memoization has its own cost, and for a cheap calculation it only makes the code more complex.

Better: Measure first. Use it only for expensive calculations or for the props of a memo child, or turn on React Compiler.

Mistake: Using useCallback without a memo child

Why? If the child is not wrapped in memo, it re-renders anyway, so a stable function gives no benefit.

Better: Pair useCallback with a memo child or with an effect dependency.

Mistake: Leaving things out of the dependency array

Why? The cached function holds on to old state, which is called a stale closure.

Better: Put every value used inside the function in the deps, and follow the eslint-plugin-react-hooks warnings.

Interview tip

The difference in one line: useMemo caches a value, useCallback caches a function. Also mention that since React Compiler arrived, new code needs much less manual memoization.

7. What to learn before and after

Final quick revision

PointuseMemouseCallback
What it cachesThe result of a calculationThe function itself
SignatureuseMemo(() => value, deps)useCallback(fn, deps)
Common pairingExpensive calculationmemo child, effect deps
With the CompilerOften not neededOften not needed