Sub topic 8
Using the compiler with React 17, 18 and 19
This chapter assumes you have already installed the Babel plugin as shown in the previous chapter. You should be comfortable editing a Babel or Vite config and reading package.json. The chapter covers which React versions the compiler supports, what the target option changes, and what can go wrong when the option and the installed React version disagree.
Why the React version matters
The compiler does not rewrite your components into plain JavaScript that needs no help from React. It emits code that stores computed values in a per-component cache and reads them back on later renders. That cache is provided by a small runtime function, exposed as c. It is a wrapper around React's internal memo cache hook.
React 19 ships this function in the react/compiler-runtime entry point. React 17 and 18 do not have it. For those versions, the compiler needs a separate package, react-compiler-runtime, that supplies an equivalent implementation.
The compiled output differs only in where it imports the cache function from:
- React 19:
import { c as _c } from "react/compiler-runtime"; - React 17 and 18:
import { c as _c } from "react-compiler-runtime";
The rest of the output has the same shape. The target option selects which import the plugin writes.
Support matrix
| React version | Supported | Runtime package needed | `target` value |
|---|---|---|---|
| 19 | Yes, default | None, uses `react/compiler-runtime` | `'19'` or omitted |
| 18 | Yes | `react-compiler-runtime` | `'18'` |
| 17 | Yes | `react-compiler-runtime` | `'17'` |
| 16 and earlier | No | Not applicable | Not applicable |
React 19 is the best-supported configuration, because the runtime comes from React itself and moves in step with it. React 17 and 18 work, but you take on one more dependency and one more setting that must match your installed React.
Check your installed version rather than trusting package.json. Run this from the project root:
npm ls react react-domIf several copies of React appear in the tree, such as one in a library under node_modules, fix that first. The compiled code runs against whichever copy of React your app resolves, and a mismatch there causes problems that look like compiler bugs.
Setting up for React 19
With React 19 you install only the plugin. No runtime package and no target option are needed:
npm install --save-dev babel-plugin-react-compiler@latest// babel.config.js
module.exports = {
plugins: [
// The compiler plugin must run first in the plugin list.
['babel-plugin-react-compiler', {}],
],
};The empty options object means target takes its default, '19'. If you prefer to be explicit, { target: '19' } is equivalent and documents your intent.
Setting up for React 17 and 18
Two extra steps apply. First, install the runtime package as a regular dependency, not a dev dependency. The compiled code imports it when your app runs, so it must be in the production bundle:
npm install --save-dev babel-plugin-react-compiler@latest
npm install react-compiler-runtime@latestSecond, set target to your major React version:
// babel.config.js
module.exports = {
plugins: [
[
'babel-plugin-react-compiler',
{
// Use '17' for React 17.
target: '18',
},
],
],
};In a Vite project the option goes inside the plugin entry, as a tuple:
// 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' }]],
},
}),
],
});Note the double brackets. The outer array is the plugin list, and the inner array pairs the plugin name with its options. Writing the bare string 'babel-plugin-react-compiler' is what you do when you have no options, so it is easy to leave the target out by accident when copying from an example.
What the compiled output looks like per version
You can inspect the result by running Babel directly on a file. Create a small component:
// Greeting.jsx
export function Greeting({ name }) {
return <h1>Hello, {name}</h1>;
}Compile it with a React 18 target:
npx babel Greeting.jsx --plugins=babel-plugin-react-compiler --presets=@babel/preset-reactThe command above uses the default target. To compile for React 18, run it through a config file with target: '18' as shown earlier. In the output, look at the first lines. With the 18 target you will find an import from react-compiler-runtime. With the default target you will find one from react/compiler-runtime. The component body then calls _c(2) or a similar number, which allocates a cache with that many slots.
This is a quick, reliable check that your configuration is doing what you intended, before you build the whole app.
Common errors from a mismatch
Most version problems come from the target value and the installed React disagreeing. They fail at build or startup, not subtly.
| Symptom | Likely cause | Fix |
|---|---|---|
| Build error that `react/compiler-runtime` cannot be resolved | React 18 or 17 installed, but `target` is `'19'` or omitted | Set `target` to `'18'` or `'17'` and install `react-compiler-runtime` |
| Build error that `react-compiler-runtime` cannot be resolved | `target` is `'18'` but the package is not installed, or only installed in a different workspace package | Install it as a dependency of the package that is built |
| Works in development, fails in production | Runtime package installed as a dev dependency and pruned from the production install | Move it to `dependencies` |
| Runtime error about a missing export or hook | Two copies of React, or a stale lockfile resolving an older React | Dedupe with `npm ls react` and reinstall |
In a monorepo, install the runtime in the package whose code is compiled. If a shared package is compiled and consumed by apps with different React versions, that is the library situation in the next section.
Libraries that ship compiled code
If you publish a component library, you can compile it before publishing and ship the optimized output. Your users then get memoization without running the compiler themselves. Two decisions follow from this chapter's topic:
- Set
targetto the lowest React major version your library supports. If you support 17 and later, compile with'17'. - List
react-compiler-runtimeunder your library'sdependencieswhen the target is below 19, so consumers receive it automatically.
Consumers on React 19 who use a library compiled with target '17' still work, because the library imports react-compiler-runtime, which you declared as a dependency. They do pay for one small extra package. If you only support 19, compile with the default and skip the dependency.
Test the published package against each React major you claim to support, since the runtime source differs between them.
Behavior differences to expect
The compiler's memoization semantics are the same across versions. A component that bails out on React 19 bails out on 18, because the decision depends on the Rules of React, not on the runtime. Still, a few things differ in practice:
- Newer React features are not retrofitted. Compiling an app does not give React 17 the features of React 19. Compilation changes how values are cached, not which APIs exist.
- Upgrade effects. Moving from 18 to 19 means changing
targetand removingreact-compiler-runtime. Do both in the same commit as the React upgrade, so the import source and the installed React never disagree. - Strict Mode and development checks. Compiled components still render twice in Strict Mode during development. A component that is not pure shows the problem on every version.
A quick verification checklist
After setting target, confirm four things:
npm ls reactshows one React version, and it matchestarget.- For React 17 and 18,
react-compiler-runtimeis independencies. - A compiled file's import line points at the runtime source you expect.
- React DevTools shows the "Memo" badge on a compiled component. The next chapters cover DevTools and the lint rules in more detail.
Exercise
A team runs React 18.2 and adds ['babel-plugin-react-compiler', {}] to their Babel config. The build fails because react/compiler-runtime cannot be resolved. What two changes fix it, and where should the second package be listed in package.json?
Show answer
First, set the plugin option target: '18' so the compiled code imports from react-compiler-runtime instead of react/compiler-runtime. Second, install react-compiler-runtime, and list it under dependencies rather than devDependencies, because the compiled code imports it at runtime and it must be present in the production bundle.
Exercise
You maintain a component library that supports React 17, 18 and 19. Which target value do you compile with, and what must the library declare in its package.json?
Show answer
Compile with target: '17', the lowest supported major version. Declare react-compiler-runtime under dependencies, so consumers on any supported React version get the runtime the compiled code imports.