Sub topic 8
Using the compiler with React 17, 18 and 19
This chapter assumes you can read a Babel or Vite config and that you know what useMemo does. You should also know which React version your project uses. If you don't, check the react entry in package.json.
Why the version matters
Compiled code needs somewhere to store cached values between renders. React 19 has a built-in cache API for this, and the compiler's output calls it directly. React 17 and 18 don't have that API, so the compiler needs two things from you:
- A
targetoption that tells it which React version the output must run on. - A small runtime package that supplies the cache API those versions lack.
The compiler supports React 17, 18 and 19. React 19 needs no extra setup.
The three cases
| React version | `target` option | Extra package |
|---|---|---|
| 19 | Omit it, or set `'19'` | None |
| 18 | `'18'` | `react-compiler-runtime` |
| 17 | `'17'` | `react-compiler-runtime` |
Install the plugin as a development dependency, because it only runs at build time:
npm install --save-dev babel-plugin-react-compiler@latestFor React 17 or 18, also install the runtime. Use a regular dependency, not a dev dependency, because the compiled code imports it when the app runs:
npm install react-compiler-runtime@latestConfiguring the target
For React 19, the minimal configuration from the setup chapter is enough. For React 18, add the target option:
// babel.config.js
module.exports = {
plugins: [
[
'babel-plugin-react-compiler',
{
// Needed for React 17 or 18; omit for React 19.
target: '18',
},
],
],
};With Vite, pass the same options as a second element of the plugin entry:
// vite.config.js
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
export default defineConfig({
plugins: [
react({
babel: {
plugins: [['babel-plugin-react-compiler', { target: '18' }]],
},
}),
],
});What the compiled code imports
The imports in the output differ by target. On React 19, the compiled file imports its cache function from react/compiler-runtime. On React 17 and 18, it imports the same function from react-compiler-runtime. Your source code doesn't change. You can check which one a build uses by opening a built file and searching for compiler-runtime.
Common mistakes
- Setting a target that doesn't match the installed React. A
'19'build running on React 18 calls a cache API that doesn't exist, so the app crashes at render. Matchtargetto the version users actually run. - Adding the runtime as a dev dependency. Some deploy steps prune dev dependencies, and the production bundle then loses the runtime.
- Setting
target: '18'on a React 19 project. This still works, but it loads the extra runtime package for no benefit. Omit the option on React 19. - Upgrading React later and leaving the old target. After you move from 18 to 19, remove the option and uninstall the runtime package.
The compiler's behavior is the same on every supported version. The Rules of React, the lint rules and the 'use memo' and 'use no memo' directives all apply the same way. Only the runtime import changes.
Exercise
A team runs React 18.2 in production. They installed babel-plugin-react-compiler as a dev dependency and enabled it with no options. List the two changes they need to make, and say which dependency type each one uses.
Show answer
First, set target: '18' in the plugin options. Second, install react-compiler-runtime. The runtime must be a regular dependency, because compiled code imports it at run time. The plugin itself stays a dev dependency, because it only runs during the build.
Exercise
Your project upgrades from React 18 to React 19. What cleanup should you do in the compiler configuration?
Show answer
Remove the target: '18' option, because React 19 is the default target. Then uninstall react-compiler-runtime, because React 19 provides the cache API itself.