React Compiler Runtime
react-compiler-runtime is a foundational library that provides the necessary primitives and infrastructure for the experimental React Compiler to operate effectively at runtime. It underpins the compiler's ability to automatically memoize components and hooks, thereby optimizing rendering performance without manual `useMemo` or `useCallback` declarations. The package itself is largely internal, meant to be consumed by the compiled output of the React Compiler rather than directly imported by application developers. As of version 1.0.0, its release cadence is tightly coupled with the development and release cycles of the broader React ecosystem, particularly the React Compiler and React core library (currently React 19 is under active development). Its key differentiator is enabling a future where React applications achieve optimal performance with significantly reduced boilerplate, shifting the responsibility of memoization from the developer to the build toolchain.
Common errors
-
Module not found: Can't resolve 'react-compiler-runtime'
cause The `react-compiler-runtime` package is not installed or incorrectly referenced in your project's build configuration. This is common when setting up the React Compiler.fix`npm install react-compiler-runtime` or `yarn add react-compiler-runtime`. For libraries targeting React < 19, ensure it's a direct dependency and included in the final bundle. -
Uncaught Error: Invalid hook call. Hooks can only be called inside of the body of a function component.
cause This common React error can occur if the `react-compiler-runtime` or the React Compiler itself is misconfigured, leading to an incorrect build output that violates React's Rules of Hooks.fixVerify your React Compiler setup, ensuring proper transpilation and React version compatibility in your Babel/Vite configuration. Check your `react` and `react-dom` versions. Ensure your code adheres to React's Rules of Hooks. -
Peer dependency 'react' (^17.0.0 || ^18.0.0 || ^19.0.0 || ^0.0.0-experimental) not met.
cause Your installed React version does not satisfy the `react-compiler-runtime`'s peer dependency requirements, which can lead to instability or undefined behavior.fixUpgrade or downgrade your `react` and `react-dom` packages to match the required range, e.g., `npm install react@latest react-dom@latest` or `npm install react@18 react-dom@18`.
Warnings
- breaking Direct usage of `react-compiler-runtime` APIs (like the internal `_c` hook) is highly discouraged. Its internal structure is subject to frequent change without adherence to semver, as these APIs are intended for the React Compiler's generated output, not for application developers.
- gotcha The React Compiler (and thus its runtime) is currently experimental. Its behavior, stability, and integration methods are still evolving, and breaking changes or unexpected performance characteristics may occur. Consider its use primarily for libraries or projects that can tolerate this level of churn.
- gotcha Ensure your `react` peer dependency (`^17.0.0 || ^18.0.0 || ^19.0.0 || ^0.0.0-experimental`) is met. Mismatched React versions can lead to runtime errors or incorrect compiler behavior, as the runtime is designed to integrate deeply with specific React core functionalities and may polyfill APIs for older versions.
- deprecated The `eslint-plugin-react-hooks` package, closely related to the compiler ecosystem, saw a `component-hook-factories` rule accidentally removed in v7.1.0 and then re-added as a deprecated no-op in v7.1.1. This indicates the rapid evolution of tooling around the React Compiler and the need to keep related packages up-to-date.
Install
-
npm install react-compiler-runtime -
yarn add react-compiler-runtime -
pnpm add react-compiler-runtime
Imports
- _c
import { c as _c } from 'react-compiler-runtime'; - ReactCompilerRuntime
import 'react-compiler-runtime';
- createMemoCache
/* No direct import for application developers */
Quickstart
import React, { useState, useMemo } from 'react';
import ReactDOM from 'react-dom/client';
// This component uses a memoized calculation.
// With the React Compiler, the `useMemo` call for `expensiveCalculation`
// might be automatically inserted or optimized, relying on the
// `react-compiler-runtime` under the hood to manage the memoization cache.
function MyOptimizedComponent() {
const [count, setCount] = useState(0);
const [multiplier, setMultiplier] = useState(2);
const expensiveCalculation = useMemo(() => {
console.log('Calculating expensive value...');
let result = 0;
for (let i = 0; i < 1000000; i++) {
result += i;
}
return result * count * multiplier;
}, [count, multiplier]);
return (
<div>
<h1>React Compiler Runtime Demo</h1>
<p>Count: {count}</p>
<button onClick={() => setCount(c => c + 1)}>Increment Count</button>
<p>Multiplier: {multiplier}</p>
<button onClick={() => setMultiplier(m => m + 1)}>Increment Multiplier</button>
<p>Expensive Calculation Result: {expensiveCalculation}</p>
<p>
The React Compiler, enabled by `react-compiler-runtime`, aims to automatically memoize
parts of your components, making manual `useMemo` and `useCallback` often unnecessary.
This example demonstrates a pattern the compiler would optimize.
</p>
</div>
);
}
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<MyOptimizedComponent />);
// To run, save as index.tsx, add react and react-dom to package.json,
// and set up a build process (e.g., with Vite or Create React App).
// Ensure your build setup includes the React Compiler plugin if you want to
// observe its effects beyond this conceptual example.