Sub topic 5
What compiled output looks like
This chapter assumes you know function components, useState, and why React re-renders a component. It also assumes you have read the earlier chapters on the Rules of React and on what the compiler does. Here you will read the code the compiler produces, so you can recognize it in build output and DevTools.
The compiler rewrites your function
The compiler runs as a Babel plugin. It takes each component or hook it can prove safe and returns a new function that does the same work, plus a cache. You keep writing plain code. The cache replaces the useMemo, useCallback and memo calls you would otherwise write by hand.
Here is a small component as you write it:
import { useState } from 'react';
export function List({ items }) {
const [query, setQuery] = useState('');
const visible = items.filter((i) => i.name.includes(query));
return (
<ul>
{visible.map((item) => (
<li key={item.id}>{item.name}</li>
))}
</ul>
);
}This is a simplified version of what the compiler produces for it:
import { c as _c } from 'react/compiler-runtime';
import { useState } from 'react';
export function List(t0) {
const $ = _c(5);
const { items } = t0;
const [query] = useState('');
let t1;
if ($[0] !== items || $[1] !== query) {
t1 = items.filter((i) => i.name.includes(query));
$[0] = items;
$[1] = query;
$[2] = t1;
} else {
t1 = $[2];
}
const visible = t1;
let t2;
if ($[3] !== visible) {
t2 = (
<ul>
{visible.map((item) => (
<li key={item.id}>{item.name}</li>
))}
</ul>
);
$[3] = visible;
$[4] = t2;
} else {
t2 = $[4];
}
return t2;
}Real output differs by compiler version and has extra bookkeeping, but the shape is the same.
Reading the output
_c(5)comes fromreact/compiler-runtime. It returns an array of 5 slots, called$, that React keeps for this component instance across renders.- Slots hold two kinds of values. Some are the inputs of a computation (
items,query). Others are its results (t1,t2). - Each
ifis a dependency check. If every input is identical (!==is false for all of them), the block is skipped and the cached result is reused. - Names such as
t0,t1andt2are temporaries the compiler generates. - Props arrive as one parameter (
t0) and are destructured inside.
The slot count and dependencies come from analysis of your code, not from a list you write. That is why the Rules of React matter. If you mutate a prop during render, the compiler cannot know when a cached value is still valid.
Stable values and the sentinel
Values that never change, such as an inline function with no captured variables, are computed once. The compiler guards them with a sentinel instead of comparing inputs:
if ($[0] === Symbol.for('react.memo_cache_sentinel')) {
$[0] = () => console.log('clicked');
}Fresh cache slots start out holding this symbol. The first render fills the slot, and later renders find a real value there and skip the block. This is how a handler like handleSelect gets a stable identity without useCallback.
Seeing it yourself
You can inspect output by running Babel with the plugin on one file. You can also paste code into the React Compiler Playground, which is a web page that shows the output next to your input. Both are useful when a component re-renders more than you expect.
Exercise
In the compiled List, the parent re-renders and passes a new items array with the same contents, but query is unchanged. Does the items.filter(...) block run again? Why?
Show answer
Yes. The check $[0] !== items compares by reference, and a new array has a different identity even when its contents match. The block recomputes, stores the new items and visible, and the JSX block then re-runs because visible is also a new array.
Exercise
What does the sentinel Symbol.for('react.memo_cache_sentinel') tell the compiled code about a cache slot?
Show answer
A slot still holding the sentinel has never been filled. The block runs once on the first render, stores its value, and later renders reuse that value without comparing inputs.