Sub topic 13
The React Compiler and the future of manual memoization
This chapter assumes you know React.memo, useMemo, useCallback, dependency arrays, and referential equality from the earlier chapters. It explains how the React Compiler automates much of that work and what you still need to do yourself.
What the compiler does
The React Compiler is a build-time tool. It runs as a Babel plugin, reads your components and hooks, and rewrites them so that values and JSX are cached between renders. You write plain code. The compiler inserts the caching that you would otherwise write with useMemo, useCallback, and memo.
import { useState } from 'react';
function Row({ item, onSelect }) {
return <li onClick={() => onSelect(item.id)}>{item.name}</li>;
}
export function List({ items }) {
const [selectedId, setSelectedId] = useState(null);
const visible = items.filter((item) => item.visible);
return (
<>
<p>Selected: {selectedId ?? 'none'}</p>
<ul>
{visible.map((item) => (
<Row key={item.id} item={item} onSelect={setSelectedId} />
))}
</ul>
</>
);
}Without the compiler, visible is a new array on every render. The compiler caches it and recomputes it only when items changes. It also caches each <Row> element, so rows whose inputs have not changed are skipped. The code contains no memo, useMemo, or useCallback.
What the compiler requires
The compiler is only safe if your code follows the Rules of React. Components and hooks must be pure during render. You must not mutate props, state, or values created during a previous render. Hooks must be called unconditionally at the top level.
When the compiler finds code it cannot prove safe, it skips that component and leaves it unchanged. It does not break your app. Install eslint-plugin-react-hooks to get diagnostics that show which components were skipped and why.
Adopting it
You enable the compiler in your build tool. For Vite, add the Babel plugin:
// vite.config.js
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
export default defineConfig({
plugins: [
react({
babel: {
plugins: ['babel-plugin-react-compiler'],
},
}),
],
});React 19 supports the compiler directly. React 17 and 18 can use it with an additional runtime package and a target option in the plugin settings.
You can also adopt it gradually. Set the plugin's compilationMode to 'annotation' and it compiles only functions that opt in with a directive. A component that causes trouble can opt out:
function LegacyWidget({ data }) {
'use no memo';
// Code the compiler should leave alone.
return <div>{data.label}</div>;
}What changes in your daily code
Most of what you wrote by hand becomes unnecessary in compiled code. The compiler also does things manual memoization cannot. It can cache a value after an early return, which a hook cannot do because hooks cannot be called conditionally. It also caches at a finer grain than a whole component.
Some manual memoization remains useful:
- Effect dependencies. If an object or function is a dependency of
useEffect, an explicituseMemooruseCallbackgives you a guarantee about when the effect re-runs. The React team recommends keeping these in code that already has them. - Existing code. Leave your current
useMemoanduseCallbackcalls in place. The compiler handles them, and removing them can change compiled output. - Uncompilable components. Components that break the rules, or that you opt out, still need hand-written memoization.
Summary of the decision
| Situation | Approach |
|---|---|
| New project on React 19 | Enable the compiler, write plain code, profile |
| Existing code with `useMemo` and `useCallback` | Keep them, and add the compiler gradually |
| Component the compiler skips or breaks | Fix the rule violation, or add `'use no memo'` |
| Effect that must not re-run | Keep an explicit `useMemo` or `useCallback` |
Exercise
A teammate enables the React Compiler and wants to delete every useCallback in the codebase in the same pull request. Give two reasons to advise against it.
Show answer
First, some callbacks are effect dependencies, and an explicit useCallback guarantees when the effect re-runs, so removing it can change behavior. Second, removing hand-written memoization can change the compiled output, and components the compiler skips (because they break the Rules of React or use 'use no memo') would lose their only caching. Enable the compiler first, profile, and clean up in small steps.
Exercise
The compiler reports that it skipped a component. What is the most likely cause, and what should you do first?
Show answer
The component probably breaks a Rule of React, for example by mutating a prop or state during render. Check the eslint-plugin-react-hooks diagnostics, fix the violation, and only then consider 'use no memo' or manual memoization as a fallback.