Sub topic 7
Setting up the Babel plugin
This chapter assumes you know function components, useState, useMemo and useCallback. It also assumes you have used a build tool such as Vite, Next.js or Babel directly. By the end you will have the React Compiler running in a project and know how to confirm the build picks it up.
What the plugin does
The React Compiler ships as a Babel plugin named babel-plugin-react-compiler. Babel runs it while your code is built. The plugin reads each component and hook, then emits the same code with memoization added. Nothing changes at runtime except that the output skips work when inputs have not changed.
Install the package
Install the plugin as a development dependency. Pin an exact version, because the compiler's output can change between releases.
npm install --save-dev --save-exact babel-plugin-react-compiler@latestConfigure Babel
With a plain Babel setup, add the plugin to babel.config.js.
// babel.config.js
module.exports = {
plugins: [
// The compiler plugin must run first in the plugin list.
['babel-plugin-react-compiler', {}],
],
};The compiler must run first. It needs the original source shape, and other plugins may rewrite the code into a form it cannot analyze.
Configure Vite
In Vite, pass the plugin through @vitejs/plugin-react.
// vite.config.js
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
export default defineConfig({
plugins: [
react({
babel: {
plugins: ['babel-plugin-react-compiler'],
},
}),
],
});Restart the dev server after the change. Cached output from before the plugin was added can hide the effect.
Check the result on a component
Take a component with no manual memoization.
import { useState } from 'react';
function Row({ item, onSelect }) {
return <li onClick={() => onSelect(item.id)}>{item.name}</li>;
}
export function List({ items }) {
const [query, setQuery] = useState('');
const [selectedId, setSelectedId] = useState(null);
const visible = items.filter((i) => i.name.includes(query));
const handleSelect = (id) => setSelectedId(id);
return (
<>
<input value={query} onChange={(e) => setQuery(e.target.value)} />
<p>Selected: {selectedId}</p>
<ul>
{visible.map((item) => (
<Row key={item.id} item={item} onSelect={handleSelect} />
))}
</ul>
</>
);
}Without the compiler, typing in the input re-renders every Row, because handleSelect is a new function on each render. With the compiler, handleSelect and each Row element are cached. Changing selectedId no longer re-renders rows whose props did not change.
Compiler options
The second element of the plugin entry is an options object. Two options matter most at first.
| Option | Purpose | Example |
|---|---|---|
| `compilationMode` | Chooses which functions are compiled | `'annotation'` compiles only functions marked `'use memo'` |
| `target` | Sets the React version the output runs on | `'18'` for React 18 |
// babel.config.js
module.exports = {
plugins: [
[
'babel-plugin-react-compiler',
{
// Compile only functions that opt in with a "use memo" directive.
compilationMode: 'annotation',
// Needed for React 17 or 18; omit for React 19.
target: '18',
},
],
],
};Common setup errors
- Plugin not first. Another Babel plugin runs before the compiler. Move the compiler to the top of the list.
- Missing runtime. On React 17 or 18, the app fails to import the compiler's helper. Install
react-compiler-runtimeand settarget. - Build tool does not use Babel. Some setups compile with SWC or esbuild instead. The Babel plugin has no effect there, so use the tool's own supported integration or switch the plugin in your configuration.
- Stale cache. Clear your bundler cache and restart if nothing seems to change.
Exercise
You add babel-plugin-react-compiler to a Babel config that already lists another plugin first. The app builds, but components are not memoized as expected. What is the likely fix, and what option would you set to run on React 18?
Show answer
Move the compiler plugin to the first position in the plugins list so it sees the original source. For React 18, set target: '18' in the plugin options and install react-compiler-runtime.