Sub topic 3
What the React Compiler does
This chapter assumes you know function components, props and state, and the useState, useMemo and useCallback hooks. You should also know that React re-renders a component when its state or its parent changes. If you have used React.memo, you already know the problem this chapter addresses.
The problem the compiler solves
When a parent re-renders, React re-renders every child by default. It also creates new objects, arrays and functions on each render. A child wrapped in memo compares its props by reference. A new function or object on every render makes that comparison fail, so the child re-renders anyway.
Manual memoization fixes this. The cost is that you must decide what to wrap, write dependency arrays correctly, and keep them current as the code changes. Here is a list component written that way:
import { memo, useCallback, useMemo, useState } from 'react';
const Row = memo(function Row({ item, onSelect }) {
return <li onClick={() => onSelect(item.id)}>{item.name}</li>;
});
export function List({ items }) {
const [query, setQuery] = useState('');
const [selectedId, setSelectedId] = useState(null);
const visible = useMemo(
() => items.filter((i) => i.name.includes(query)),
[items, query]
);
const handleSelect = useCallback((id) => setSelectedId(id), []);
return (
<>
<input value={query} onChange={(e) => setQuery(e.target.value)} />
<p>Selected: {selectedId}</p>
<ul>
{visible.map((item) => (
<Row key={item.id} item={item} onSelect={handleSelect} />
))}
</ul>
</>
);
}What the compiler is
The React Compiler is a build-time tool. It reads your components and hooks, works out which values depend on which inputs, and rewrites the code so that values are reused when their inputs have not changed. It runs as a Babel plugin during your build. Nothing about it runs as a separate service, and you do not change how you call React.
With the compiler, you can write the same component without the wrappers:
import { useState } from 'react';
function Row({ item, onSelect }) {
return <li onClick={() => onSelect(item.id)}>{item.name}</li>;
}
export function List({ items }) {
const [query, setQuery] = useState('');
const [selectedId, setSelectedId] = useState(null);
const visible = items.filter((i) => i.name.includes(query));
const handleSelect = (id) => setSelectedId(id);
return (
<>
<input value={query} onChange={(e) => setQuery(e.target.value)} />
<p>Selected: {selectedId}</p>
<ul>
{visible.map((item) => (
<Row key={item.id} item={item} onSelect={handleSelect} />
))}
</ul>
</>
);
}The compiler caches visible and handleSelect itself. It also skips re-rendering work in Row when its inputs are unchanged.
How the pieces fit together
What it relies on
The compiler can only reuse a value safely if the same inputs always produce the same result. That is why it depends on the Rules of React: components and hooks must be pure during render, and props and state must not be mutated. Code that follows the rules gets compiled. Code that breaks them is skipped, and the rest of your app is unaffected. A later chapter covers the rules and how to find violations.
// Breaks the Rules of React: mutates a prop during render.
function Bad({ items }) {
items.sort();
return <ul>{items.map((i) => <li key={i}>{i}</li>)}</ul>;
}
// Fixed: copy first, then sort the copy.
function Good({ items }) {
const sorted = [...items].sort();
return <ul>{sorted.map((i) => <li key={i}>{i}</li>)}</ul>;
}Bad changes the array its parent owns on every render. Caching its output would be unsafe, so the compiler does not touch it.
Common misunderstandings
- The compiler does not make renders free. It avoids repeating work when inputs are unchanged.
- It does not require you to delete existing
useMemo,useCallbackormemo. Existing code keeps working. - It does not fix slow code by itself. An expensive calculation that must run on every change still runs.
Exercises
Exercise
A parent re-renders and passes onSelect={(id) => setSelectedId(id)} to a child wrapped in memo. Why does the child still re-render, and what does the compiler change?
Show answer
The inline arrow function is a new reference on every render, so the shallow prop comparison done by memo fails. The compiler caches the function so the same reference is reused while its inputs are unchanged, and the child can skip work without you writing useCallback.
Exercise
Why does the compiler skip a component that calls items.sort() on a prop?
Show answer
Sorting in place mutates a value the component does not own, so render is not pure. The compiler cannot safely reuse results for such code, so it leaves the component uncompiled. Sorting a copy, [...items].sort(), fixes this.