Sub topic 11
Confirming a component is compiled
The compiler fails quietly. If it skips a component, your app still works and you get no memoization. This chapter covers four ways to check what the compiler did, from quickest to most detailed. The first is the React DevTools badge. The second is the compiled output. The third is the compiler's own logger. The fourth is a Profiler comparison that shows whether re-renders actually dropped.
This chapter assumes you have already installed the Babel plugin (see the setup chapter). It also assumes you know what the lint rules report, and you can read a React DevTools component tree.
Why verification is a separate step
The compiler works one component or hook at a time. When it finds code it cannot prove safe, such as a prop mutation during render, it leaves that function unchanged. This is called a bailout. The rest of the file is still compiled.
Three things follow:
- A successful build says nothing about which components were optimized.
- A project can have the plugin installed and still compile only part of its code.
- A misconfigured plugin, for example one that runs after another transform, can compile nothing at all.
Verify after the first setup. Verify again after you change compiler options or upgrade the compiler.
Check 1: the "Memo ✨" badge in React DevTools
The fastest check is the React DevTools browser extension, version 5.0 or later. It marks every component the compiler optimized with a sparkle badge next to its name.
- Run your app in development mode.
- Open the browser DevTools and select the Components tab.
- Find your component in the tree.
- Look for
Memo ✨beside the name.
A component with the badge has compiler-generated memoization. A component without it was either skipped, excluded by your configuration, or never reached by the plugin.
The badge also appears for components wrapped in React.memo, but it shows without the sparkle there. Look for the sparkle, not just the word "Memo".
Reading a result
Take the List component from the compiler example. You would expect:
| Component | Badge | Meaning |
|---|---|---|
| `List` | `Memo ✨` | Compiled |
| `Row` | `Memo ✨` | Compiled |
| A component with `'use no memo'` | none | Opted out on purpose |
| A component that mutates props | none | Bailout; check the lint output |
If every component lacks the badge, suspect the build setup first. If only some lack it, suspect the code in those components.
Check 2: look at the compiled output
The badge is a summary. The output is the proof. A compiled function imports a cache helper from react/compiler-runtime and checks each cached value against the previous render's inputs.
Compiled output looks roughly like this (shortened, and the exact shape changes between compiler versions):
import { c as _c } from "react/compiler-runtime";
function Row(t0) {
const $ = _c(6);
const { item, onSelect } = t0;
let t1;
if ($[0] !== item.id || $[1] !== onSelect) {
t1 = () => onSelect(item.id);
$[0] = item.id;
$[1] = onSelect;
$[2] = t1;
} else {
t1 = $[2];
}
// ...the JSX for the <li> is cached in the same way
}Three things identify compiled code:
- The import from
react/compiler-runtime. With thetargetoption set to'17'or'18', the import comes fromreact-compiler-runtimeinstead. - A call like
_c(6), which allocates a cache with a fixed number of slots. if ($[0] !== value)blocks that recompute only when an input changed.
You can see this output in the browser's Sources panel, since it is the code the bundler served. For a quicker check, run a single file through Babel from a script. The next script compiles one file and prints what the compiler decided.
// check-compiled.mjs
// Usage: node check-compiled.mjs src/List.jsx
// Needs: npm install -D @babel/core @babel/preset-react babel-plugin-react-compiler
import fs from 'node:fs';
import babel from '@babel/core';
const file = process.argv[2];
if (!file) {
console.error('Usage: node check-compiled.mjs <file>');
process.exit(1);
}
const source = fs.readFileSync(file, 'utf8');
const result = babel.transformSync(source, {
filename: file,
babelrc: false,
configFile: false,
sourceType: 'module',
presets: ['@babel/preset-react'],
plugins: [
[
'babel-plugin-react-compiler',
{
logger: {
logEvent(filename, event) {
console.log(event.kind, event.fnName ?? '');
},
},
},
],
],
});
const compiled = result.code.includes('compiler-runtime');
console.log(compiled ? 'Output uses the compiler runtime.' : 'No compiled code found.');Plugins run before presets in Babel, so the compiler sees your JSX before it is transformed. That ordering matters. If JSX or other syntax has already been rewritten, the compiler can miss components.
Check 3: the compiler logger
The logger option in the script above is available in your real config too. The compiler calls logEvent once per function it processes. The kind field tells you what happened. Common values are:
| Event kind | Meaning |
|---|---|
| `CompileSuccess` | The function was compiled |
| `CompileError` | The compiler found a problem and bailed out |
| `CompileSkip` | The function was skipped, for example by an opt-out or compilation mode |
Event shapes are not a stable public API and can change between compiler versions. Print event.kind and the function name, and check the package's release notes before building tooling on other fields.
A useful pattern is to collect events during a build and print a one-line summary:
// babel.config.js
const counts = {};
module.exports = {
plugins: [
[
'babel-plugin-react-compiler',
{
logger: {
logEvent(filename, event) {
counts[event.kind] = (counts[event.kind] ?? 0) + 1;
},
},
},
],
],
};
process.on('exit', () => {
console.log('React Compiler events:', counts);
});This works for one build process. Bundlers that spawn worker processes will print one summary per worker, so treat the numbers as indicative.
Check 4: lint output
The lint rule from the earlier chapter reports the same problems the compiler hits. It is the best way to learn why a component has no badge. Run ESLint over the file and read the message on the line it points to.
The two tools agree on cause but run at different times. ESLint runs in your editor, while the compiler runs during the build. A clean lint run is good evidence that a component can be compiled, but only the badge or the output proves that it was.
Putting the checks together
Confirming the effect with the Profiler
The badge proves compilation. The Profiler proves benefit. Use the React DevTools Profiler tab with this procedure:
- Start a recording.
- Type in the search box of the
Listexample. - Stop the recording and open the flame graph.
- Check which
Rowcomponents rendered.
When the input changes query, rows whose item is unchanged should be grayed out, meaning they did not render. Enable "Record why each component rendered" in the Profiler settings if you want the reason for each render.
Run the same recording once with the compiler enabled and once disabled. Many projects see little difference for small components, because rendering a few cheap elements was never expensive. The compiler matters most for large lists, expensive subtrees, and components that pass objects or functions to memoized children.
Common reasons a badge is missing
- Wrong plugin order. The compiler must run first in the plugin list. Other transforms that rewrite the code earlier can hide components from it.
- A production-only setup. The plugin was added only to a production config, so development builds show no badges.
- Annotation mode. In
compilationMode: 'annotation', only functions with'use memo'are compiled. Everything else has no badge by design. - A cached build. Some bundlers cache Babel results. Clear the cache after changing Babel options.
- An old DevTools version. Versions before 5.0 do not show the badge even when compilation worked. Check the output instead.
- A rule violation. The component mutates props, state, or a value created before render. The lint rule names the line.
- Not a component or hook. The compiler targets functions it recognizes as components (capitalized name, returns JSX) or hooks (name starting with
use). A helper function with a lowercase name that returns JSX is not compiled.
A quick regression check
You can turn verification into an automated test. In CI, run the compiler over a file you care about and assert that it produced a CompileSuccess event for it. Reuse the logger from Check 3, and fail the job if the count of CompileError events grows. That catches a new rule violation in code review instead of in a profiling session months later.
Set panicThreshold: 'all_errors' in the plugin options if you want the build itself to fail on any compiler error. The default is to skip the problem function and continue. Failing the build is useful in a codebase you have already cleaned up, but it is disruptive while you are still migrating.
Exercise
You enable the compiler on a Vite project. The Components tab in DevTools shows no sparkle badge on any component, but the app runs fine. List two things you would check first, and one check that would tell you whether the compiler ran at all.
Show answer
First, check that babel-plugin-react-compiler is passed to @vitejs/plugin-react through the babel.plugins option and is first in the list, and that compilationMode is not 'annotation' without any 'use memo' directives. Also check that your DevTools extension is version 5.0 or later. To tell whether the compiler ran at all, inspect the served code or run a file through Babel and look for an import from react/compiler-runtime and calls to _c(). Absence of those means nothing was compiled, so the problem is in the setup and not in your components.
Exercise
A component has the Memo ✨ badge, yet the Profiler shows it re-rendering on every keystroke in an unrelated input. Does the badge mean something is wrong with the compiler?
Show answer
No. The badge only shows the component was compiled. It re-renders when its inputs change or when a parent re-renders and passes values the compiler cannot prove are unchanged. Use "Record why each component rendered" in the Profiler to see which prop or state changed, then check whether the parent creates a new object each render in code the compiler skipped.