The React Compiler and Render Purity
Build, explain and debug React 19 interfaces: components, rendering, state, effects, identity, forms and actions, server components, concurrency, performance and testing, ending with architecture and typing work on real component challenges.
What you will be able to do
#- Explain what the React Compiler optimizes automatically.
- Write components that satisfy the compiler's purity assumptions.
- Recognize code that breaks the compiler and how to fix it.
Overview
#What the compiler does
The React Compiler analyzes components and hooks and inserts memoization automatically where it is safe. It can replace hand-written memo, useMemo and useCallback in many cases, reducing the need to reason about referential identity.
The purity requirement
The compiler assumes components are pure: given the same props and state they return the same output and do not mutate values outside their scope. Impure code makes the compiler's memoization incorrect, so it either skips optimizing or produces a bug.
// impure: mutates a prop
function List({ items }: { items: Item[] }) {
items.sort();
return <ul>{items.map((i) => <li key={i.id}>{i.label}</li>)}</ul>;
}
Common violations
Mutating props or state in place, writing to module-level variables during render, reading and writing refs during render, and side effects such as network calls or logging in render all break purity. Move mutations to event handlers and effects.
// pure: copy before sorting
const sorted = [...items].sort(compare);
Rules of React
The compiler enforces the Rules of React: components and hooks must be pure, hooks must be called at the top level, and state must be updated immutably. Following these rules makes a component both correct under StrictMode and optimizable.
Opting out
The compiler lets you opt a component out with a directive when it cannot prove safety. Use it as a temporary measure while you fix the underlying impurity.
Pitfall: relying on the compiler for correctness
The compiler optimizes, it does not fix bugs. An impure component is a bug whether or not the compiler is enabled. StrictMode double-invocation surfaces these bugs by running render twice.
Pitfall: measuring the wrong thing
After enabling the compiler, re-profile. It reduces re-renders, but a component that is slow because of expensive work inside a single render is unaffected.
Exercises
#Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.
Exercise 1Predict
What is wrong with this component under the compiler?
Read the code, then choose the best answer. The reference explanation stays hidden until you respond. stimulus.tsxfunction Cart({ items }: { items: Item[] }) { items.push({ id: 'x', price: 1 }); return <p>{items.length}</p>; }Exercise 2Implement
Make this component pure so the compiler can optimize it.
starter.tsxfunction Sorted({ items }: { items: Item[] }) { items.sort((a, b) => a.rank - b.rank); return <ul>{items.map((i) => <li key={i.id}>{i.label}</li>)}</ul>; }Reveal the reference answer
reference.tsxfunction Sorted({ items }: { items: Item[] }) { const sorted = [...items].sort((a, b) => a.rank - b.rank); return <ul>{sorted.map((i) => <li key={i.id}>{i.label}</li>)}</ul>; }Authored expected output: The component sorts a copy and never mutates the prop.
Copying the array before sorting keeps the prop unchanged, so render is pure and the compiler can memoize the result safely.
Exercise 3Debug
This component logs once in the console but twice under StrictMode. Explain and fix.
starter.tsxfunction Chart({ data }: { data: number[] }) { analytics.track('render', data.length); return <svg>...</svg>; }Reveal the reference answer
reference.tsxfunction Chart({ data }: { data: number[] }) { useEffect(() => { analytics.track('render', data.length); }, [data.length]); return <svg>...</svg>; }Authored expected output: The analytics call runs once per committed change.
StrictMode double-invokes render to surface impurity. Calling analytics during render is a side effect, so it runs twice. Moving it to an effect ties it to commits.
Exercise 4Explain
Explain why the React Compiler requires pure components and what purity means here.
starter.tsx// same inputs, same output, no side effectsReveal the reference answer
reference.tsxPurity means a component returns the same output for the same props and state and does not mutate anything outside its scope or cause side effects during render. The compiler relies on this to memoize safely: if it caches a result, that result must still be correct later, which only holds if the render had no hidden dependencies or mutations. Impure code, such as mutating a prop, writing a module variable or logging during render, makes the compiler's assumptions false, so it either skips optimizing or produces incorrect output. Purity is also what makes StrictMode's double render and concurrent rendering safe.Authored expected output: A definition of purity plus why the compiler's memoization depends on it.
The answer should tie purity to the safety of caching, and note that purity is a correctness requirement independent of the compiler.
Common misconceptions
#- The compiler fixes impure components. It optimizes; impurity is still a bug.
- The compiler makes memo and useCallback unnecessary everywhere. It handles many cases, but stable references can still matter.
- Mutating props is fine if the result looks right. It breaks purity and the compiler's assumptions.
- Enabling the compiler removes all performance work. Expensive single renders are unaffected.
Follow-up practice
#- react-profiling-and-devtools
- react-component-architecture
Your progress
#Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.
Verification: authored. Answers and outputs on this page are authored reference material, not executed results.