logo by @sawaratsuki1004
React
v19.2
Learn
Reference
Community
Blog

Is this page useful?

في هذه الصفحة

  • Overview
  • Prerequisites
  • Installation
  • Basic Setup
  • Babel
  • Vite
  • Next.js
  • React Router
  • Webpack
  • Expo
  • Metro (React Native)
  • Rspack
  • Rsbuild
  • ESLint Integration
  • Verify Your Setup
  • Check React DevTools
  • Check Build Output
  • Troubleshooting
  • Opting out specific components
  • Next Steps

    البدأ

  • بداية سريعة
    • شرح تطبيقي: لعبة تيك تاك تو
    • التفكير على طريقة React
  • التثبيت
    • إنشاء تطبيق React
    • بناء تطبيق React من الصفر
    • إضافة React إلى مشروع موجود بالفعل
  • الإعداد
    • تجهيز المحرر
    • استخدام TypeScript
    • أدوات مطوري React
  • React Compiler
    • مقدمة
    • التثبيت
    • التبني التدريجي
    • تصحيح الأخطاء واستكشاف المشاكل
  • تعلم React

  • وصف واجهة المستخدم (UI)
    • مكونك الأول (Component)
    • استيراد وتصدير المكونات (Components)
    • كتابة ترميز البناء بـ JSX
    • JavaScript في JSX باستخدام الأقواس المنحنية
    • تمرير الخصائص (Props) إلى مكون
    • التصيير الشرطي (Conditional Rendering)
    • تصيير القوائم (Rendering Lists)
    • الحفاظ على نقاء المكونات (Pure Components)
    • واجهتك المستخدم كشجرة (UI Tree)
  • إضافة التفاعلية (Interactivity)
    • الاستجابة للأحداث (Events)
    • الحالة (State): ذاكرة المُكَوِّن
    • التصيير والالتزام (Render and Commit)
    • الحالة (State) كلقطة
    • إضافة سلسلة من تحديثات الحالة (State) إلى قائمة انتظار
    • تحديث الكائنات (Objects) في الحالة
    • تحديث المصفوفات (Arrays) في الحالة
  • إدارة State
    • التفاعل مع Input باستخدام State
    • اختيار بنية State
    • مشاركة State بين Components
    • الحفاظ على State وإعادة ضبطها
    • استخراج منطق State إلى Reducer
    • تمرير البيانات بشكل عميق باستخدام Context
    • التوسع باستخدام Reducer و Context
  • مخارج الطوارئ (Escape Hatches)
    • الإشارة إلى القيم باستخدام Refs
    • التلاعب بـ DOM باستخدام Refs
    • التزامن مع Effects
    • قد لا تحتاج إلى Effect
    • دورة حياة Reactive Effects
    • فصل Events عن Effects
    • إزالة اعتماديات Effect
    • إعادة استخدام المنطق باستخدام Custom Hooks
تعلم React
React Compiler

Installation

This guide will help you install and configure React Compiler in your React application.

You will learn

  • How to install React Compiler
  • Basic configuration for different build tools
  • How to verify your setup is working

Prerequisites

React Compiler is designed to work best with React 19, but it also supports React 17 and 18. Learn more about React version compatibility.

Installation

Install React Compiler as a devDependency:

Terminal
npm install -D babel-plugin-react-compiler@latest

Or with Yarn:

Terminal
yarn add -D babel-plugin-react-compiler@latest

Or with pnpm:

Terminal
pnpm install -D babel-plugin-react-compiler@latest

Basic Setup

React Compiler is designed to work by default without any configuration. However, if you need to configure it in special circumstances (for example, to target React versions below 19), refer to the compiler options reference.

The setup process depends on your build tool. React Compiler includes a Babel plugin that integrates with your build pipeline.

مأزق

React Compiler must run first in your Babel plugin pipeline. The compiler needs the original source information for proper analysis, so it must process your code before other transformations.

Babel

Create or update your babel.config.js:

module.exports = { plugins: [ 'babel-plugin-react-compiler', // must run first! // ... other plugins ], // ... other config };

Vite

If you use Vite, you can add the plugin to vite-plugin-react:

// vite.config.js import { defineConfig } from 'vite'; import react from '@vitejs/plugin-react'; export default defineConfig({ plugins: [ react({ babel: { plugins: ['babel-plugin-react-compiler'], }, }), ], });

Alternatively, if you prefer a separate Babel plugin for Vite:

Terminal
npm install -D vite-plugin-babel

// vite.config.js import babel from 'vite-plugin-babel'; import { defineConfig } from 'vite'; import react from '@vitejs/plugin-react'; export default defineConfig({ plugins: [ react(), babel({ babelConfig: { plugins: ['babel-plugin-react-compiler'], }, }), ], });

Next.js

Please refer to the Next.js docs for more information.

React Router

Install vite-plugin-babel, and add the compiler’s Babel plugin to it:

Terminal
npm install vite-plugin-babel

// vite.config.js import { defineConfig } from "vite"; import babel from "vite-plugin-babel"; import { reactRouter } from "@react-router/dev/vite"; const ReactCompilerConfig = { /* ... */ }; export default defineConfig({ plugins: [ reactRouter(), babel({ filter: /\.[jt]sx?$/, babelConfig: { presets: ["@babel/preset-typescript"], // if you use TypeScript plugins: [ ["babel-plugin-react-compiler", ReactCompilerConfig], ], }, }), ], });

Webpack

A community webpack loader is now available here.

Expo

Please refer to Expo’s docs to enable and use the React Compiler in Expo apps.

Metro (React Native)

React Native uses Babel via Metro, so refer to the Usage with Babel section for installation instructions.

Rspack

Please refer to Rspack’s docs to enable and use the React Compiler in Rspack apps.

Rsbuild

Please refer to Rsbuild’s docs to enable and use the React Compiler in Rsbuild apps.

ESLint Integration

React Compiler includes an ESLint rule that helps identify code that can’t be optimized. When the ESLint rule reports an error, it means the compiler will skip optimizing that specific component or hook. This is safe: the compiler will continue optimizing other parts of your codebase. You don’t need to fix all violations immediately. Address them at your own pace to gradually increase the number of optimized components.

Install the ESLint plugin:

Terminal
npm install -D eslint-plugin-react-hooks@latest

If you haven’t already configured eslint-plugin-react-hooks, follow the installation instructions in the readme. The compiler rules are available in the recommended-latest preset.

The ESLint rule will:

  • Identify violations of the Rules of React
  • Show which components can’t be optimized
  • Provide helpful error messages for fixing issues

Verify Your Setup

After installation, verify that React Compiler is working correctly.

Check React DevTools

Components optimized by React Compiler will show a “Memo ✨” badge in React DevTools:

  1. Install the React Developer Tools browser extension
  2. Open your app in development mode
  3. Open React DevTools
  4. Look for the ✨ emoji next to component names

If the compiler is working:

  • Components will show a “Memo ✨” badge in React DevTools
  • Expensive calculations will be automatically memoized
  • No manual useMemo is required

Check Build Output

You can also verify the compiler is running by checking your build output. The compiled code will include automatic memoization logic that the compiler adds automatically.

import { c as _c } from "react/compiler-runtime"; export default function MyApp() { const $ = _c(1); let t0; if ($[0] === Symbol.for("react.memo_cache_sentinel")) { t0 = <div>Hello World</div>; $[0] = t0; } else { t0 = $[0]; } return t0; }

Troubleshooting

Opting out specific components

If a component is causing issues after compilation, you can temporarily opt it out using the "use no memo" directive:

function ProblematicComponent() { "use no memo"; // Component code here }

This tells the compiler to skip optimization for this specific component. You should fix the underlying issue and remove the directive once resolved.

For more troubleshooting help, see the debugging guide.

Next Steps

Now that you have React Compiler installed, learn more about:

  • React version compatibility for React 17 and 18
  • Configuration options to customize the compiler
  • Incremental adoption strategies for existing codebases
  • Debugging techniques for troubleshooting issues
  • Compiling Libraries guide for compiling your React library
السابقمقدمة
التاليالتبني التدريجي

Copyright © Meta Platforms, Inc
no uwu plz
uwu?
Logo by@sawaratsuki1004
تعلم React
بداية سريعة
التثبيت
وصف واجهة المستخدم (UI)
إضافة التفاعلية
إدارة State
مخارج الطوارئ
مرجع API
React APIs
React DOM APIs
المجتمع
ميثاق السلوك
تعرف على الفريق
المساهمون في التوثيق
شكر وتقدير
المزيد
المدونة
React Native
الخصوصية
الشروط
module.exports = {
plugins: [
'babel-plugin-react-compiler', // must run first!
// ... other plugins
],
// ... other config
};
// vite.config.js
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';

export default defineConfig({
plugins: [
react({
babel: {
plugins: ['babel-plugin-react-compiler'],
},
}),
],
});
// vite.config.js
import babel from 'vite-plugin-babel';
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';

export default defineConfig({
plugins: [
react(),
babel({
babelConfig: {
plugins: ['babel-plugin-react-compiler'],
},
}),
],
});
// vite.config.js
import { defineConfig } from "vite";
import babel from "vite-plugin-babel";
import { reactRouter } from "@react-router/dev/vite";

const ReactCompilerConfig = { /* ... */ };

export default defineConfig({
plugins: [
reactRouter(),
babel({
filter: /\.[jt]sx?$/,
babelConfig: {
presets: ["@babel/preset-typescript"], // if you use TypeScript
plugins: [
["babel-plugin-react-compiler", ReactCompilerConfig],
],
},
}),
],
});
import { c as _c } from "react/compiler-runtime";
export default function MyApp() {
const $ = _c(1);
let t0;
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
t0 = <div>Hello World</div>;
$[0] = t0;
} else {
t0 = $[0];
}
return t0;
}
function ProblematicComponent() {
"use no memo";
// Component code here
}