Sub topic 11
Confirming a component is compiled
This chapter assumes you have installed the Babel plugin as in the setup chapter, and that you know how to open React DevTools in a browser. The compiler runs silently at build time. If it skips a component, nothing breaks and nothing warns you, so you need a way to check.
Check 1: the Memo badge in React DevTools
React DevTools marks compiled components in the Components tab. A component the compiler optimized shows a "Memo ✨" badge next to its name.
- Install the React DevTools browser extension and keep it up to date. Older versions don't show the badge.
- Run your app in development mode.
- Open the Components tab and find the component in the tree.
- Look for the badge.
Take the List and Row components from the earlier chapter, written without memo, useMemo or useCallback. If the compiler is working, both show the badge. If they don't, either the compiler didn't run or it skipped them.
Check 2: read the build output
You can also look at the code your bundler produces. Compiled components import a helper from react/compiler-runtime and keep their cached values in an array. The exact output varies by compiler version, but the shape is similar to this:
import { c as _c } from "react/compiler-runtime";
function Row(t0) {
const $ = _c(4);
const { item, onSelect } = t0;
let t1;
if ($[0] !== item.id || $[1] !== item.name || $[2] !== onSelect) {
t1 = <li onClick={() => onSelect(item.id)}>{item.name}</li>;
$[0] = item.id;
$[1] = item.name;
$[2] = onSelect;
$[3] = t1;
} else {
t1 = $[3];
}
return t1;
}The call to _c(4) allocates a cache with four slots. The if compares the current inputs with the stored ones. If nothing changed, the component returns the stored element. If you see _c( and a react/compiler-runtime import in the output for a component, it was compiled. If you see your original source, it wasn't.
Check 3: lint feedback before you build
The lint rules from eslint-plugin-react-hooks report code the compiler cannot handle, such as mutating props during render. Run ESLint first, because a lint error predicts a skipped component.
// Flagged: mutates a prop during render, so the compiler skips it.
function Bad({ items }) {
items.sort();
return <ul>{items.map((i) => <li key={i}>{i}</li>)}</ul>;
}Fix it by sorting a copy ([...items].sort()), then check again for the badge.
A repeatable workflow
Common reasons the badge is missing
| Symptom | Likely cause |
|---|---|
| No badge on any component | The plugin is not in the Babel config, the dev server was not restarted, or the build tool uses a different transform |
| Badge missing on one component | The component breaks a Rule of React and was skipped |
| Badge missing with `compilationMode: 'annotation'` | The function has no `'use memo'` directive |
| Badge missing on a component with `'use no memo'` | You opted it out on purpose |
| Badge missing in a library component | The code in `node_modules` was not compiled |
When one component lacks the badge, the next chapter covers bailouts and how to debug them.
Exercise
After enabling the compiler with compilationMode: 'annotation', none of your components show the Memo badge in DevTools, yet ESLint reports no problems. What is the most likely cause, and how do you fix it?
Show answer
In annotation mode the compiler only compiles functions that opt in. Add the 'use memo' directive as the first statement of each component you want compiled, restart the dev server, and check for the badge again.