React Compiler vs useMemo: Do You Still Need useMemo?

The React Compiler is a build-time tool that adds memoization (caching) to your components automatically, so in most code you no longer need to write useMemo, useCallback or React.memo by hand. They still exist as an escape hatch for the cases where you want exact control.

This is a popular interview question right now, because "when do I use useMemo?" used to have a simple answer and now it's more nuanced. Let's start with what these tools actually do, then see what changes.

First, what is memoization?

Memoization means remembering the result of some work, so you don't repeat it when the inputs haven't changed. It's like writing the answer to a math problem on a sticky note: if someone asks the same question again, you read the note instead of recalculating.

In React, there are three common tools for this:

useMemo vs useCallback vs React.memo

  • useMemo caches a value, like the result of filtering a big list.
  • useCallback caches a function, so it's the same function object on every render (until its dependencies change).
  • React.memo wraps a component so React skips re-rendering it when its props are the same as last time.

Why we needed them in the first place

By default, when a component re-renders, React re-renders all of its children too, and it runs every line of the component again. (If that's new to you, read why a component re-renders.) For most components that's fine, because rendering is fast.

Memoization became useful in two situations:

  1. A calculation is slow. You wrap it in useMemo so it only reruns when its inputs change.
  2. You pass something to a memoized child. A function or object created inside a component is a new object on every render, even if it looks identical. That breaks React.memo on the child, because the prop is "different" every time. So you wrap the function in useCallback and the object in useMemo.

Here's what that looked like:

function ProductList({ products, query }) {
  const visible = useMemo(
    () => products.filter((p) => p.name.includes(query)),
    [products, query]
  );

  const handleSelect = useCallback((id) => {
    console.log('selected', id);
  }, []);

  return <List items={visible} onSelect={handleSelect} />;
}

It works, but it's a lot of extra code. You have to remember the dependency arrays, and if you forget one, you get a stale value.

What the React Compiler changes

The React Compiler runs at build time. It reads your components, works out which values depend on which inputs, and inserts the caching for you. Your source code stays plain:

function ProductList({ products, query }) {
  const visible = products.filter((p) => p.name.includes(query));

  const handleSelect = (id) => {
    console.log('selected', id);
  };

  return <List items={visible} onSelect={handleSelect} />;
}

How the React Compiler adds memoization at build time

Step by step:

  1. You write normal code. No hooks for caching, no dependency arrays.
  2. The compiler analyzes it during the build. It figures out what each value depends on.
  3. The compiled output only recalculates when needed. If products and query didn't change, the filtered list is reused.

The React docs also point out a case where the compiler does better than manual code. An inline arrow function in JSX, like onClick={() => ...}, defeats a useCallback wrapper that's built the old way, but the compiler handles it correctly, with or without the arrow function.

So, do you still need useMemo?

Here's the guidance in the React docs, in plain words:

When to still use useMemo with the React Compiler

  • For new code, rely on the compiler for memoization. Add useMemo or useCallback only where you need precise control.
  • For existing code, you can leave your current useMemo and useCallback calls in place. Removing them can change what the compiler produces, so only do it if you can verify nothing gets worse.
  • As an escape hatch, useMemo and useCallback keep working with the compiler. A common reason is a value used as an effect dependency: if you need that value to stay exactly the same unless something specific changes, you can memoize it yourself so the effect doesn't fire more often than you want.

useMemo vs useCallback vs React.memo

useMemouseCallbackReact.memo
What is it?A hookA hookA wrapper for a component
What does it cache?A calculated valueA functionA component's render output
Typical useSlow calculation, stable objectStable function for a child or an effectSkip re-rendering a child
Runs where?Inside a componentInside a componentAround a component

Two details worth knowing:

  • useCallback(fn, deps) is the same as useMemo(() => fn, deps). It's just a shortcut for caching functions.
  • React.memo only helps with re-renders caused by the parent. If the memoized component's own state changes, or a context it reads changes, it still re-renders.

Turning the compiler on (and off)

You install the compiler as a Babel plugin, babel-plugin-react-compiler. It must run first in your Babel plugin list, so it sees your original code. Framework and bundler setups differ, so follow the installation page in the React docs for yours.

Two options help when adopting it in an existing app:

  • compilationMode decides which functions get compiled. The default is 'infer'. In 'annotation' mode, only functions that start with the "use memo" directive are compiled, which is handy for trying it on one component at a time.
  • "use no memo" at the top of a component or hook skips it. Treat it as a temporary workaround while you debug, because the function stays opted out until you delete the directive.

Examples

1. Manual memoization that the compiler makes unnecessary

const total = useMemo(() => items.reduce((sum, i) => sum + i.price, 0), [items]);

Problem: extra code and a dependency array to maintain, just to avoid recalculating a sum.

Why: before the compiler, this was the only way to cache a value between renders.

Fix: in new code, write the plain version and let the compiler cache it.

const total = items.reduce((sum, i) => sum + i.price, 0);

2. A useCallback that doesn't actually help

const Row = ({ item, onSelect }) => <li onClick={() => onSelect(item.id)}>{item.name}</li>;

function List({ items }) {
  const onSelect = useCallback((id) => select(id), []);
  return items.map((item) => <Row key={item.id} item={item} onSelect={onSelect} />);
}

Problem: Row isn't wrapped in React.memo, so it re-renders whenever List does, no matter how stable onSelect is.

Why: useCallback only keeps a function's identity stable. It only saves work if something downstream compares that identity, such as React.memo or a dependency array.

Fix: with the compiler, you don't need to wire this up by hand. Without it, either wrap Row in React.memo too, or remove the pointless useCallback.

3. An effect that fires too often

function Chart({ data }) {
  const options = { smooth: true, data };

  useEffect(() => {
    drawChart(options);
  }, [options]);
}

Problem: the effect runs on every render.

Why: options is a new object each render, so React sees a changed dependency every time.

Fix: the compiler can stabilize this, but if you want to be explicit and independent of the compiler, memoize the object yourself, or better, depend on the values inside it.

useEffect(() => {
  drawChart({ smooth: true, data });
}, [data]);

Interview answer

The React Compiler is a build-time tool that automatically memoizes components, values and functions, so you rarely need to write useMemo, useCallback or React.memo by hand. useMemo caches a calculated value, useCallback caches a function so its identity stays stable, and React.memo lets a component skip re-rendering when its props haven't changed. With the compiler, I write plain code for new work and leave existing memoization alone. I still reach for useMemo or useCallback as an escape hatch when I need precise control, like a stable value for an effect dependency.

Summary

  • Memoization means caching a result so you don't repeat work when the inputs are the same.
  • useMemo caches a value, useCallback caches a function, React.memo skips re-rendering a component.
  • useCallback(fn, deps) is the same as useMemo(() => fn, deps).
  • The React Compiler adds memoization automatically, at build time.
  • For new code, rely on the compiler. For existing code, leave memoization in place unless you can verify a removal is safe.
  • useMemo and useCallback still work as an escape hatch, for example for effect dependencies.
  • The compiler expects code that follows the rules of React, such as pure components and no mutation during render.
  • React.memo doesn't stop re-renders caused by the component's own state or context.
  • useMemo is a performance hint. Your code must still be correct if React recalculates the value.
  • Use "use no memo" to opt a function out while debugging, and compilationMode to adopt the compiler step by step.