Sub topic 6
Fine-grained and conditional memoization
This chapter assumes you know how React re-renders components, how useMemo and useCallback work, and what the compiler's output looks like at a high level. It explains how the React Compiler memoizes more precisely than manual hooks can.
Memoization at the level of values
With manual memoization, you choose what to wrap. You usually wrap whole components in memo and a few expensive values in useMemo. The compiler works differently. It analyzes the data flow inside a component and caches individual values and JSX fragments, each keyed to the specific inputs it reads.
Take this component from earlier in the tutorial:
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 sees that each value depends on different inputs:
| Value | Depends on |
|---|---|
| `visible` | `items`, `query` |
| `handleSelect` | nothing that changes (only the stable `setSelectedId`) |
| The `<input>` element | `query`, `setQuery` |
| The `<p>` element | `selectedId` |
| The `<ul>` element | `visible`, `handleSelect` |
When you select a row, only selectedId changes. The compiler reuses the cached visible array, the cached handleSelect function and the cached <ul> element. Only the <p> is rebuilt. Because the <ul> element is reused with the same props, React skips the work for the Row children too, and you didn't write memo.
Memoizing JSX
Manual memoization usually targets values and functions. The compiler also caches JSX. In the example above, you would need memo(Row) to prevent the rows from re-rendering. The compiler instead caches the rendered <ul> subtree as a value, so React sees the same element object and does not re-render it.
Conditional memoization
Manual hooks can't be called after an early return or inside a condition. The compiler can cache values in those cases too, because it is not bound by the Rules of Hooks for its generated cache.
function Profile({ user, theme }) {
if (!user) {
return null;
}
// With useMemo, this would have to sit above the early return.
const stats = computeStats(user.activity);
return <Dashboard stats={stats} theme={theme} />;
}Here stats is cached only on the path that reaches it, and it is recomputed only when user.activity changes. A change to theme does not recompute it. To get the same result by hand, you would have to restructure the component so that the hook runs before the return.
Instead, here is the dependency picture as a diagram.
Where precision stops
Fine-grained caching has limits:
- The compiler tracks values it can prove are safe. If a component mutates props or state during render, it skips that component. See the Rules of React chapter.
- Caching compares inputs by identity. If a parent passes a new object on every render, the cache still misses.
- It cannot know that
computeStatsis expensive. It caches by dependency, not by cost. Everything is cached, including cheap values.
Checking a result
You can view the compiled output in the React Compiler Playground at playground.react.dev. Paste the List component there and compare the cached slots with the table above. You should see separate slots for visible, handleSelect and each JSX element.
Exercise
In the List component, which cached values are reused when only selectedId changes, and why can the rows skip re-rendering without memo?
Show answer
visible, handleSelect and the <ul> element are reused, because none of their inputs (items, query) changed. The <ul> element is the same object as before, so React does not re-render the Row children inside it. Only the <p> element that reads selectedId is rebuilt.
Exercise
Why can the compiler cache stats after an early return null, when useMemo cannot be placed there?
Show answer
useMemo is a hook and must be called in the same order on every render, so it cannot come after a conditional return. The compiler generates its own cache slots and can use them on only the code path that reaches stats, so no hook is involved.