Skip to content

Commit 934ff0a

Browse files
authored
Merge pull request #671 from reactjs/sync-9467bc58
Sync with react.dev @ 9467bc5
2 parents ed02cf1 + 49de119 commit 934ff0a

File tree

2 files changed

+63
-119
lines changed

2 files changed

+63
-119
lines changed

src/content/learn/react-compiler.md

Lines changed: 63 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ title: React Compiler
55
{/* FIXME:L10N */}
66

77
<Intro>
8-
This page will give you an introduction to the new experimental React Compiler and how to try it out successfully.
8+
This page will give you an introduction to React Compiler and how to try it out successfully.
99
</Intro>
1010

1111
<Wip>
@@ -21,12 +21,28 @@ These docs are still a work in progress. More documentation is available in the
2121
</YouWillLearn>
2222

2323
<Note>
24-
React Compiler is a new experimental compiler that we've open sourced to get early feedback from the community. It still has rough edges and is not yet fully ready for production.
24+
React Compiler is a new compiler currently in Beta, that we've open sourced to get early feedback from the community. While it has been used in production at companies like Meta, rolling out the compiler to production for your app will depend on the health of your codebase and how well you’ve followed the [Rules of React](/reference/rules).
25+
26+
The latest Beta release can be found with the `@beta` tag, and daily experimental releases with `@experimental`.
2527
</Note>
2628

27-
React Compiler is a new experimental compiler that we've open sourced to get early feedback from the community. It is a build-time only tool that automatically optimizes your React app. It works with plain JavaScript, and understands the [Rules of React](/reference/rules), so you don't need to rewrite any code to use it.
29+
React Compiler is a new compiler that we've open sourced to get early feedback from the community. It is a build-time only tool that automatically optimizes your React app. It works with plain JavaScript, and understands the [Rules of React](/reference/rules), so you don't need to rewrite any code to use it.
30+
31+
The compiler also includes an [eslint plugin](#installing-eslint-plugin-react-compiler) that surfaces the analysis from the compiler right in your editor. **We strongly recommend everyone use the linter today.** The linter does not require that you have the compiler installed, so you can use it even if you are not ready to try out the compiler.
2832

29-
The compiler also includes an [eslint plugin](#installing-eslint-plugin-react-compiler) that surfaces the analysis from the compiler right in your editor. The plugin runs independently of the compiler and can be used even if you aren't using the compiler in your app. We recommend all React developers to use this eslint plugin to help improve the quality of your codebase.
33+
The compiler is currently released as `beta`, and is available to try out on React 17+ apps and libraries. To install the Beta:
34+
35+
<TerminalBlock>
36+
npm install -D babel-plugin-react-compiler@beta eslint-plugin-react-compiler@beta
37+
</TerminalBlock>
38+
39+
Or, if you're using Yarn:
40+
41+
<TerminalBlock>
42+
yarn add -D babel-plugin-react-compiler@beta eslint-plugin-react-compiler@beta
43+
</TerminalBlock>
44+
45+
If you are not using React 19 yet, please see [the section below](#using-react-compiler-with-react-17-or-18) for further instructions.
3046

3147
### What does the compiler do? {/*what-does-the-compiler-do*/}
3248

@@ -96,19 +112,9 @@ However, if `expensivelyProcessAReallyLargeArrayOfObjects` is truly an expensive
96112
So if `expensivelyProcessAReallyLargeArrayOfObjects` was used in many different components, even if the same exact items were passed down, that expensive calculation would be run repeatedly. We recommend [profiling](https://react.dev/reference/react/useMemo#how-to-tell-if-a-calculation-is-expensive) first to see if it really is that expensive before making code more complicated.
97113
</DeepDive>
98114

99-
### What does the compiler assume? {/*what-does-the-compiler-assume*/}
100-
101-
React Compiler assumes that your code:
102-
103-
1. Is valid, semantic JavaScript
104-
2. Tests that nullable/optional values and properties are defined before accessing them (for example, by enabling [`strictNullChecks`](https://www.typescriptlang.org/tsconfig/#strictNullChecks) if using TypeScript), i.e., `if (object.nullableProperty) { object.nullableProperty.foo }` or with optional-chaining `object.nullableProperty?.foo`
105-
3. Follows the [Rules of React](https://react.dev/reference/rules)
106-
107-
React Compiler can verify many of the Rules of React statically, and will safely skip compilation when it detects an error. To see the errors we recommend also installing [eslint-plugin-react-compiler](https://www.npmjs.com/package/eslint-plugin-react-compiler).
108-
109115
### Should I try out the compiler? {/*should-i-try-out-the-compiler*/}
110116

111-
Please note that the compiler is still experimental and has many rough edges. While it has been used in production at companies like Meta, rolling out the compiler to production for your app will depend on the health of your codebase and how well you've followed the [Rules of React](/reference/rules).
117+
Please note that the compiler is still in Beta and has many rough edges. While it has been used in production at companies like Meta, rolling out the compiler to production for your app will depend on the health of your codebase and how well you've followed the [Rules of React](/reference/rules).
112118

113119
**You don't have to rush into using the compiler now. It's okay to wait until it reaches a stable release before adopting it.** However, we do appreciate trying it out in small experiments in your apps so that you can [provide feedback](#reporting-issues) to us to help make the compiler better.
114120

@@ -121,7 +127,7 @@ In addition to these docs, we recommend checking the [React Compiler Working Gro
121127
Prior to installing the compiler, you can first check to see if your codebase is compatible:
122128

123129
<TerminalBlock>
124-
npx react-compiler-healthcheck@experimental
130+
npx react-compiler-healthcheck@beta
125131
</TerminalBlock>
126132

127133
This script will:
@@ -143,7 +149,7 @@ Found no usage of incompatible libraries.
143149
React Compiler also powers an eslint plugin. The eslint plugin can be used **independently** of the compiler, meaning you can use the eslint plugin even if you don't use the compiler.
144150

145151
<TerminalBlock>
146-
npm install eslint-plugin-react-compiler@experimental
152+
npm install -D eslint-plugin-react-compiler@beta
147153
</TerminalBlock>
148154

149155
Then, add it to your eslint config:
@@ -178,77 +184,75 @@ const ReactCompilerConfig = {
178184
};
179185
```
180186

181-
In rare cases, you can also configure the compiler to run in "opt-in" mode using the `compilationMode: "annotation"` option. This makes it so the compiler will only compile components and hooks annotated with a `"use memo"` directive. Please note that the `annotation` mode is a temporary one to aid early adopters, and that we don't intend for the `"use memo"` directive to be used for the long term.
182-
183-
```js {2,7}
184-
const ReactCompilerConfig = {
185-
compilationMode: "annotation",
186-
};
187-
188-
// src/app.jsx
189-
export default function App() {
190-
"use memo";
191-
// ...
192-
}
193-
```
194-
195187
When you have more confidence with rolling out the compiler, you can expand coverage to other directories as well and slowly roll it out to your whole app.
196188

197189
#### New projects {/*new-projects*/}
198190

199191
If you're starting a new project, you can enable the compiler on your entire codebase, which is the default behavior.
200192

201-
## Usage {/*installation*/}
193+
### Using React Compiler with React 17 or 18 {/*using-react-compiler-with-react-17-or-18*/}
202194

203-
### Babel {/*usage-with-babel*/}
195+
React Compiler works best with React 19 RC. If you are unable to upgrade, you can install the extra `react-compiler-runtime` package which will allow the compiled code to run on versions prior to 19. However, note that the minimum supported version is 17.
204196

205197
<TerminalBlock>
206-
npm install babel-plugin-react-compiler@experimental
198+
npm install react-compiler-runtime@beta
207199
</TerminalBlock>
208200

209-
The compiler includes a Babel plugin which you can use in your build pipeline to run the compiler.
210-
211-
After installing, add it to your Babel config. Please note that it's critical that the compiler run **first** in the pipeline:
201+
You should also add the correct `target` to your compiler config, where `target` is the major version of React you are targeting:
212202

213-
```js {7}
203+
```js {3}
214204
// babel.config.js
215-
const ReactCompilerConfig = { /* ... */ };
205+
const ReactCompilerConfig = {
206+
target: '18' // '17' | '18' | '19'
207+
};
216208

217209
module.exports = function () {
218210
return {
219211
plugins: [
220-
['babel-plugin-react-compiler', ReactCompilerConfig], // must run first!
221-
// ...
212+
['babel-plugin-react-compiler', ReactCompilerConfig],
222213
],
223214
};
224215
};
225216
```
226217

227-
`babel-plugin-react-compiler` should run first before other Babel plugins as the compiler requires the input source information for sound analysis.
218+
### Using the compiler on libraries {/*using-the-compiler-on-libraries*/}
228219

229-
React Compiler works best with React 19 RC. If you are unable to upgrade, you can install the extra `react-compiler-runtime` package which will allow the compiled code to run on versions prior to 19. However, note that the minimum supported version is 17.
220+
React Compiler can also be used to compile libraries. Because React Compiler needs to run on the original source code prior to any code transformations, it is not possible for an application's build pipeline to compile the libraries they use. Hence, our recommendation is for library maintainers to independently compile and test their libraries with the compiler, and ship compiled code to npm.
221+
222+
Because your code is pre-compiled, users of your library will not need to have the compiler enabled in order to benefit from the automatic memoization applied to your library. If your library targets apps not yet on React 19, specify a minimum [`target` and add `react-compiler-runtime` as a direct dependency](#using-react-compiler-with-react-17-or-18). The runtime package will use the correct implementation of APIs depending on the application's version, and polyfill the missing APIs if necessary.
223+
224+
Library code can often require more complex patterns and usage of escape hatches. For this reason, we recommend ensuring that you have sufficient testing in order to identify any issues that might arise from using the compiler on your library. If you identify any issues, you can always opt-out the specific components or hooks with the [`'use no memo'` directive](#something-is-not-working-after-compilation).
225+
226+
Similarly to apps, it is not necessary to fully compile 100% of your components or hooks to see benefits in your library. A good starting point might be to identify the most performance sensitive parts of your library and ensuring that they don't break the [Rules of React](/reference/rules), which you can use `eslint-plugin-react-compiler` to identify.
227+
228+
## Usage {/*installation*/}
229+
230+
### Babel {/*usage-with-babel*/}
230231

231232
<TerminalBlock>
232-
npm install react-compiler-runtime@experimental
233+
npm install babel-plugin-react-compiler@beta
233234
</TerminalBlock>
234235

235-
You should also add the correct `target` to your compiler config, where `target` is the major version of React you are targeting:
236+
The compiler includes a Babel plugin which you can use in your build pipeline to run the compiler.
236237

237-
```js {3}
238+
After installing, add it to your Babel config. Please note that it's critical that the compiler run **first** in the pipeline:
239+
240+
```js {7}
238241
// babel.config.js
239-
const ReactCompilerConfig = {
240-
target: '18' // '17' | '18' | '19'
241-
};
242+
const ReactCompilerConfig = { /* ... */ };
242243

243244
module.exports = function () {
244245
return {
245246
plugins: [
246-
['babel-plugin-react-compiler', ReactCompilerConfig],
247+
['babel-plugin-react-compiler', ReactCompilerConfig], // must run first!
248+
// ...
247249
],
248250
};
249251
};
250252
```
251253

254+
`babel-plugin-react-compiler` should run first before other Babel plugins as the compiler requires the input source information for sound analysis.
255+
252256
### Vite {/*usage-with-vite*/}
253257

254258
If you use Vite, you can add the plugin to vite-plugin-react:
@@ -275,36 +279,7 @@ export default defineConfig(() => {
275279

276280
### Next.js {/*usage-with-nextjs*/}
277281

278-
Next.js has an experimental configuration to enable the React Compiler. It automatically ensures Babel is set up with `babel-plugin-react-compiler`.
279-
280-
- Install Next.js canary, which uses React 19 Release Candidate
281-
- Install `babel-plugin-react-compiler`
282-
283-
<TerminalBlock>
284-
npm install next@canary babel-plugin-react-compiler@experimental
285-
</TerminalBlock>
286-
287-
Then configure the experimental option in `next.config.js`:
288-
289-
```js {4,5,6}
290-
// next.config.js
291-
/** @type {import('next').NextConfig} */
292-
const nextConfig = {
293-
experimental: {
294-
reactCompiler: true,
295-
},
296-
};
297-
298-
module.exports = nextConfig;
299-
```
300-
301-
Using the experimental option ensures support for the React Compiler in:
302-
303-
- App Router
304-
- Pages Router
305-
- Webpack (default)
306-
- Turbopack (opt-in through `--turbo`)
307-
282+
Please refer to the [Next.js docs](https://nextjs.org/docs/canary/app/api-reference/next-config-js/reactCompiler) for more information.
308283

309284
### Remix {/*usage-with-remix*/}
310285
Install `vite-plugin-babel`, and add the compiler's Babel plugin to it:
@@ -337,40 +312,7 @@ export default defineConfig({
337312

338313
### Webpack {/*usage-with-webpack*/}
339314

340-
You can create your own loader for React Compiler, like so:
341-
342-
```js
343-
const ReactCompilerConfig = { /* ... */ };
344-
const BabelPluginReactCompiler = require('babel-plugin-react-compiler');
345-
346-
function reactCompilerLoader(sourceCode, sourceMap) {
347-
// ...
348-
const result = transformSync(sourceCode, {
349-
// ...
350-
plugins: [
351-
[BabelPluginReactCompiler, ReactCompilerConfig],
352-
],
353-
// ...
354-
});
355-
356-
if (result === null) {
357-
this.callback(
358-
Error(
359-
`Failed to transform "${options.filename}"`
360-
)
361-
);
362-
return;
363-
}
364-
365-
this.callback(
366-
null,
367-
result.code,
368-
result.map === null ? undefined : result.map
369-
);
370-
}
371-
372-
module.exports = reactCompilerLoader;
373-
```
315+
A community Webpack loader is [now available here](https://github.com/SukkaW/react-compiler-webpack).
374316

375317
### Expo {/*usage-with-expo*/}
376318

@@ -394,11 +336,15 @@ To report issues, please first create a minimal repro on the [React Compiler Pla
394336

395337
You can also provide feedback in the React Compiler Working Group by applying to be a member. Please see [the README for more details on joining](https://github.com/reactwg/react-compiler).
396338

397-
### `(0 , _c) is not a function` error {/*0--_c-is-not-a-function-error*/}
339+
### What does the compiler assume? {/*what-does-the-compiler-assume*/}
340+
341+
React Compiler assumes that your code:
398342

399-
This occurs if you are not using React 19 RC and up. To fix this, [upgrade your app to React 19 RC](https://react.dev/blog/2024/04/25/react-19-upgrade-guide) first.
343+
1. Is valid, semantic JavaScript.
344+
2. Tests that nullable/optional values and properties are defined before accessing them (for example, by enabling [`strictNullChecks`](https://www.typescriptlang.org/tsconfig/#strictNullChecks) if using TypeScript), i.e., `if (object.nullableProperty) { object.nullableProperty.foo }` or with optional-chaining `object.nullableProperty?.foo`.
345+
3. Follows the [Rules of React](https://react.dev/reference/rules).
400346

401-
If you are unable to upgrade to React 19, you may try a userspace implementation of the cache function as described in the [Working Group](https://github.com/reactwg/react-compiler/discussions/6). However, please note that this is not recommended and you should upgrade to React 19 when possible.
347+
React Compiler can verify many of the Rules of React statically, and will safely skip compilation when it detects an error. To see the errors we recommend also installing [eslint-plugin-react-compiler](https://www.npmjs.com/package/eslint-plugin-react-compiler).
402348

403349
### How do I know my components have been optimized? {/*how-do-i-know-my-components-have-been-optimized*/}
404350

src/content/reference/react/useReducer.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,7 @@ function MyComponent() {
5252
#### Limitations et points à noter {/*caveats*/}
5353
5454
* `useReducer` est un Hook, vous ne pouvez donc l'appeler **qu'au niveau racine de votre composant** ou de vos propres Hooks. Vous ne pouvez pas l'appeler dans des boucles ou des conditions. Si vous avez besoin de le faire, extrayez un nouveau composant et déplacez-y l'état.
55-
5655
* La fonction `dispatch` a une identité stable, elle ne figure donc généralement pas dans les dépendances des Effets, mais l'inclure n'entraînera pas un déclenchement d'Effet superflu. Si le *linter* vous permet de l'omettre sans erreurs, c'est que cette omission est sans danger. [Apprenez-en davantage sur l'allègement des dépendances d'Effets](/learn/removing-effect-dependencies#move-dynamic-objects-and-functions-inside-your-effect)
57-
5856
* En Mode Strict, React **appellera deux fois votre réducteur et votre fonction d'initialisation** afin de [vous aider à détecter des impuretés accidentelles](#my-reducer-or-initializer-function-runs-twice). Ce comportement est limité au développement et n'affecte pas la production. Si votre réducteur et votre fonction d'initialisation sont pures (ce qui devrait être le cas), ça n'impactera pas votre logique. Le résultat de l'un des appels est ignoré.
5957
6058
---

0 commit comments

Comments
 (0)