Sub topic 9
Adopting the compiler gradually
This chapter assumes you can write function components with hooks and that the Babel plugin from the previous chapter is installed. You will learn how to turn the compiler on for part of a codebase first, and how to switch it off for a single component.
Why roll out in steps
The compiler changes how every compiled component behaves at runtime. Code that breaks the Rules of React may work today by accident and fail once it is memoized. A staged rollout limits the blast radius. You can enable the compiler for a few components, watch for problems, and widen the scope as confidence grows.
There are three tools for this: compilation modes, directives, and the target option.
Compilation modes
The compilationMode option controls which functions the compiler tries to compile.
| Mode | What gets compiled |
|---|---|
| `infer` (default) | Components and hooks, found by name and by what they return |
| `annotation` | Only functions marked with `'use memo'` |
Start with annotation when you want full control. The compiler then ignores everything except functions you opt in.
// 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',
},
],
],
};The target option names the React version the output must run on. React 17 and 18 also need the react-compiler-runtime package installed. React 19 needs neither.
Opting in and out with directives
A directive is a string literal placed at the top of a function body. 'use memo' opts a function in. 'use no memo' opts it out, even when the mode would otherwise compile it.
function Chart({ data }) {
'use memo';
// Compiled when compilationMode is 'annotation'.
return <canvas data-points={data.length} />;
}
function LegacyWidget({ model }) {
'use no memo';
// Skipped by the compiler, for example while a rule violation is being fixed.
model.count += 1;
return <span>{model.count}</span>;
}LegacyWidget mutates a prop-like object during render. That breaks the Rules of React, so the opt-out keeps the compiler away from it until you fix the code. Treat 'use no memo' as a temporary marker, not a permanent fix.
A rollout plan
A practical order:
- Install the plugin and set
compilationMode: 'annotation'. - Add
'use memo'to a small, well-tested part of the app, such as a leaf feature. - Run your tests and check the components in React DevTools (covered in a later chapter).
- Add
'use memo'to more directories as results stay clean. - Switch to the default
infermode. - Mark any component that still fails with
'use no memo', and keep a list so each one gets fixed.
Some teams instead start in infer mode and opt out failures. That is faster, but it exposes the whole app at once. Choose annotation when the codebase is large or lightly tested.
Common mistakes
- Forgetting the directive position. The directive must be the first statement in the function body. Placed after other code, it has no effect.
- Leaving
'use no memo'in place. An opt-out hides the problem. Fix the violation and remove the directive. - Omitting the runtime on React 17 or 18. Set
targetand installreact-compiler-runtime, or the compiled output will fail to run.
Exercise
Your app runs React 18 and you want the compiler to touch only one dashboard component for now. Which options and directive do you use, and what else must you install?
Show answer
Set compilationMode: 'annotation' and target: '18' in the plugin options. Add 'use memo' as the first statement of the dashboard component. Install react-compiler-runtime, which React 18 output needs.
Exercise
After switching to infer mode, one component that mutates its props during render breaks. What is the short-term fix and the lasting fix?
Show answer
Short term, add 'use no memo' to that component so the compiler skips it. Lasting, copy the data before changing it (for example [...items].sort()), then remove the directive.