React Compiler

Topic 2 of 4 in Performance9 min readIntermediateFree course

Overview

The previous lesson showed how memo, useMemo and useCallback can skip unnecessary work — and how fiddly they are to get right. React Compiler does it automatically. It’s a build-time tool, stable since version 1.0, that reads your components and rewrites them to memoise exactly what can be memoised. You write plain, readable React; the compiled output is optimised.

What the compiler does

You write a CourseList component that filters courses by a query and renders a List with an onPick arrow function. The compiler outputs code that recomputes the filtered list only if courses or q changed, otherwise reuses the cached value, reuses the same onPick function, and reuses the List element if its inputs are the same. No useMemo, useCallback or memo is needed, but your components must follow the Rules of React.

Consider:

JSX
function CourseList({ courses, query, onOpen }) {
  const visible = courses.filter((c) => c.title.toLowerCase().includes(query.toLowerCase()));
  return <List items={visible} onPick={(id) => onOpen(id)} />;
}

Without the compiler, every render recomputes visible, creates a new onPick function, and re-renders List. With the compiler, the output (simplified) behaves like:

  • if courses and query are unchanged → reuse the previous visible,
  • if onOpen is unchanged → reuse the previous onPick,
  • if visible and onPick are unchanged → reuse the previous <List> element, so React skips re-rendering it.

It memoises at a finer grain than you’d normally bother to by hand — individual values and JSX elements, even after early returns.

Setting it up with Vite

Vite 8 projects use @vitejs/plugin-react 6, which offers a ready-made Babel preset for the compiler:

Terminal
npm install -D @rolldown/plugin-babel @babel/core babel-plugin-react-compiler
JavaScript
// vite.config.js
import babel from '@rolldown/plugin-babel';
import react, { reactCompilerPreset } from '@vitejs/plugin-react';
import { defineConfig } from 'vite';

export default defineConfig({
  plugins: [react(), babel({ presets: [reactCompilerPreset()] })],
});

That’s it — restart the dev server. In React DevTools, compiled components show a Memo ✨ badge next to their name.

@vitejs/plugin-react also has an experimental compiler: true option that uses a faster Rust port of the compiler. The Babel setup above is the stable route.

Other setups:

  • Next.js: set reactCompiler: true in next.config.js (and install babel-plugin-react-compiler).
  • React Router framework mode / other Babel-based tools: add babel-plugin-react-compiler to the Babel plugins.
  • React 17 or 18: supported with the target option and the react-compiler-runtime package.

Linting: the compiler’s companion

The compiler’s checks are also available as lint rules in eslint-plugin-react-hooks (version 7+ includes the compiler-powered rules in its recommended config). They flag code the compiler can’t optimise — mutating props, reading refs during render, calling hooks conditionally, setting state during render — while you type. Enable them even before turning the compiler on; they catch real bugs.

JavaScript
// eslint.config.js
import reactHooks from 'eslint-plugin-react-hooks';
import { defineConfig } from 'eslint/config';

export default defineConfig([
  reactHooks.configs.flat.recommended,
]);

The Rules of React

The compiler can only optimise code it can prove is safe. That means following the Rules of React — which you’ve been learning throughout this course:

  1. Components and hooks must be pure. Same props and state → same output; no side effects during render.
  2. Props and state are immutable. Never mutate them; create new values.
  3. Hook arguments and return values are immutable once passed.
  4. Don’t mutate values after passing them to JSX.
  5. Only call hooks at the top level, from React functions.
  6. Don’t read or write ref.current during render.

When the compiler detects a violation, it skips that component and leaves it exactly as written — your app keeps working, that component just isn’t memoised. The lint rules report the same problem:

JSX
// ✗ Assigns to a prop — detected: lint error, component left uncompiled
function CourseTitle({ course }) {
  course.title = course.title.trim();
  return <h2>{course.title}</h2>;
}

But not every violation can be detected. A mutating method call on a prop slips through:

JSX
// ✗ Mutates a prop with sort() — NOT detected, so it gets compiled
function CourseList({ courses, sortBy }) {
  courses.sort((a, b) => a[sortBy].localeCompare(b[sortBy]));
  return <ul>{courses.map((c) => <li key={c.id}>{c.title}</li>)}</ul>;
}

The compiler assumes courses can’t change unless its identity changes, so it caches the list and only rebuilds it when a new courses array arrives. Change sortBy and the component re-renders — but shows the old order. Code that “worked” without the compiler now has a real bug. The fix is the same as always: don’t mutate, derive.

JSX
// ✓ Derives a new array — correct with or without the compiler
function CourseList({ courses, sortBy }) {
  const sorted = courses.toSorted((a, b) => a[sortBy].localeCompare(b[sortBy]));
  return <ul>{sorted.map((c) => <li key={c.id}>{c.title}</li>)}</ul>;
}

That’s why following the rules matters more than ever: the lint rules and the compiler catch a lot, but not everything.

Adopting it gradually

For an existing codebase:

  1. Turn on the lint rules and fix the violations they report.
  2. Enable the compiler and test your app — code that broke the rules in ways the tools can’t detect may have “worked by accident” and behave differently once memoised (like the sort() example above).
  3. If one component causes trouble, opt it out with the "use no memo" directive while you fix it:
JSX
function LegacyWidget() {
  'use no memo';
  // …
}

You can also start with compilationMode: 'annotation', which only compiles components marked with "use memo", then switch to compiling everything.

What about existing useMemo and useCallback?

You don’t need to delete them — the compiler works alongside them. In new code, you can write without them and let the compiler handle memoisation. Two cases still call for manual tools:

  • Effect dependencies where you need a guaranteed-stable value regardless of compilation — keep useMemo/useCallback there, or restructure.
  • Truly expensive work that you want to control explicitly.

What the compiler doesn’t do

The compiler reduces unnecessary re-rendering and recomputation. It doesn’t:

  • make a genuinely expensive render cheaper (sorting 50,000 rows is still slow — memoised or not, it runs whenever the data changes),
  • shrink your bundle or speed up network requests,
  • fix slow typing caused by rendering huge lists — that’s what transitions and virtualisation are for (next lesson).

The big win is simplicity. With React Compiler on, you write the obvious code, and performance follows — as long as your components play by the Rules of React.

Exercise

Enable the compiler and fix a violation

Add React Compiler to your Vite project with @rolldown/plugin-babel and reactCompilerPreset, and turn on the React Hooks lint rules. Then fix the Rules of React violation in this component: it mutates a prop while rendering. Neither the linter nor the compiler can detect this kind of mutation, so with the compiler on, the list would keep its old order when sortBy changes. Confirm in React DevTools that the component shows the Memo ✨ badge.

Terminal
# 1. install and configure the compiler (vite.config.js)

# 2. fix this component:
# function CourseList({ courses, sortBy }) {
#   courses.sort((a, b) => a[sortBy].localeCompare(b[sortBy]));
#   return <ul>{courses.map((c) => <li key={c.id}>{c.title}</li>)}</ul>;
# }

Summary

  • React Compiler is a build-time tool that automatically memoizes components, values and functions.
  • It removes the need for most manual memo, useMemo and useCallback, and is often more precise than hand-written memoisation.
  • In a Vite 8 project, enable it with @rolldown/plugin-babel and the reactCompilerPreset from @vitejs/plugin-react.
  • It relies on the Rules of React: pure rendering, no mutation of props or state, hooks called correctly.
  • Detected rule violations make it skip a component; undetected ones (like props.items.sort()) can turn into stale-UI bugs.