Sub topic 12
When the compiler skips code and how to debug it
This chapter assumes you can read basic React components and hooks, and that you have already set up the Babel plugin and the ESLint rules from earlier chapters. It covers what happens when the compiler cannot safely optimize a component, and how to find out why.
What a bailout is
The compiler analyzes each component and hook on its own. If it finds code it cannot prove safe to memoize, it skips that one function and leaves it as you wrote it. This is called a bailout. The rest of the file is still compiled, and your app keeps working. A bailout costs you performance, not correctness.
Common causes:
- Mutating props, state, or values created during render.
- Reading or writing a ref's
.currentduring render. - Calling hooks conditionally or inside loops.
- Syntax the compiler does not yet support, such as some
try/catchpatterns around values it needs to track. - Mutating a value after it has been passed to JSX or a hook.
Step 1: Ask the linter
The lint rules report the same problems the compiler finds. Run ESLint first, because its messages point to a line and name the rule that was broken.
Here is a component that triggers a bailout:
function Bad({ items }) {
items.sort();
return <ul>{items.map((i) => <li key={i}>{i}</li>)}</ul>;
}items is a prop, and sort() changes it in place. The compiler cannot assume the output depends only on the inputs, so it skips the component. The fix is to sort a copy:
function Good({ items }) {
const sorted = [...items].sort();
return <ul>{sorted.map((i) => <li key={i}>{i}</li>)}</ul>;
}After the change, the lint error disappears and the compiler can process the function.
Step 2: Check DevTools
A clean lint run does not prove a component was compiled. Open React DevTools and look at the Components tab. Compiled components show a "Memo ✨" badge next to their name. If a component you expect to be compiled has no badge, it was skipped or never reached by the plugin.
If nothing in the whole app has a badge, check the setup instead:
- Confirm
babel-plugin-react-compileris listed, and listed first among the Babel plugins. - Check that your file extensions and folders are processed by Babel.
- If you set
compilationMode: 'annotation', only functions with'use memo'are compiled. - Restart the dev server. Stale caches can hide a config change.
Step 3: Isolate the problem
Sometimes the compiler compiles a component, but the compiled version behaves differently from the original. This usually means the code relied on something that breaks the Rules of React, and the lint rules did not flag it. Use the 'use no memo' directive to find out:
function LegacyWidget({ model }) {
'use no memo';
// Skipped by the compiler while the rule violation is investigated.
model.count += 1;
return <span>{model.count}</span>;
}The directive goes at the top of the function body. Add it, and see whether the bug disappears. If it does, the compiler is not at fault. The component has a hidden violation, such as mutation during render, that the compiler's assumptions exposed. Fix the violation, then remove the directive.
Common gotchas
| Symptom | Likely cause | What to do |
|---|---|---|
| No badge in DevTools | Plugin not running, or annotation mode | Check the Babel config and mode |
| Lint error on a ref | Reading `ref.current` during render | Read refs in event handlers or effects |
| Component behaves differently when compiled | Hidden mutation or impure render | Use `'use no memo'`, then fix the code |
| Only some components compiled | Others have rule violations | Fix lint errors one by one |
Work from the lint output, not from guesses. Each reported rule maps to a specific pattern, and fixing that pattern is almost always enough to bring the component back into the compiled set.
Exercise
A component shows no "Memo ✨" badge in DevTools, but ESLint reports no errors. Name two things you would check next.
Show answer
First, check the plugin setup: that babel-plugin-react-compiler is in the Babel config and runs first, and that the dev server was restarted. Second, check the compilation mode: in annotation mode, only functions marked with 'use memo' are compiled, so an unmarked component will never get a badge.
Exercise
After enabling the compiler, a component shows stale data. Adding 'use no memo' fixes it. What does this tell you, and what should you do next?
Show answer
The component probably has a hidden Rules of React violation, such as mutating a value during render, which the compiler's assumptions exposed. Find and fix the mutation or impurity, then remove the directive so the component can be compiled again.