You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: src/content/learn/react-compiler.md
+63-117
Original file line number
Diff line number
Diff line change
@@ -3,7 +3,7 @@ title: React Compiler
3
3
---
4
4
5
5
<Intro>
6
-
This page will give you an introduction to the new experimental React Compiler and how to try it out successfully.
6
+
This page will give you an introduction to React Compiler and how to try it out successfully.
7
7
</Intro>
8
8
9
9
<Wip>
@@ -19,12 +19,28 @@ These docs are still a work in progress. More documentation is available in the
19
19
</YouWillLearn>
20
20
21
21
<Note>
22
-
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.
22
+
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).
23
+
24
+
The latest Beta release can be found with the `@beta` tag, and daily experimental releases with `@experimental`.
23
25
</Note>
24
26
25
-
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.
27
+
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.
28
+
29
+
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.
26
30
27
-
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.
31
+
The compiler is currently released as `beta`, and is available to try out on React 17+ apps and libraries. To install the Beta:
If you are not using React 19 yet, please see [the section below](#using-react-compiler-with-react-17-or-18) for further instructions.
28
44
29
45
### What does the compiler do? {/*what-does-the-compiler-do*/}
30
46
@@ -94,19 +110,9 @@ However, if `expensivelyProcessAReallyLargeArrayOfObjects` is truly an expensive
94
110
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.
95
111
</DeepDive>
96
112
97
-
### What does the compiler assume? {/*what-does-the-compiler-assume*/}
98
-
99
-
React Compiler assumes that your code:
100
-
101
-
1. Is valid, semantic JavaScript
102
-
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`
103
-
3. Follows the [Rules of React](https://react.dev/reference/rules)
104
-
105
-
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).
106
-
107
113
### Should I try out the compiler? {/*should-i-try-out-the-compiler*/}
108
114
109
-
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).
115
+
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).
110
116
111
117
**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.
112
118
@@ -119,7 +125,7 @@ In addition to these docs, we recommend checking the [React Compiler Working Gro
119
125
Prior to installing the compiler, you can first check to see if your codebase is compatible:
120
126
121
127
<TerminalBlock>
122
-
npx react-compiler-healthcheck@experimental
128
+
npx react-compiler-healthcheck@beta
123
129
</TerminalBlock>
124
130
125
131
This script will:
@@ -141,7 +147,7 @@ Found no usage of incompatible libraries.
141
147
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.
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.
180
-
181
-
```js {2,7}
182
-
constReactCompilerConfig= {
183
-
compilationMode:"annotation",
184
-
};
185
-
186
-
// src/app.jsx
187
-
exportdefaultfunctionApp() {
188
-
"use memo";
189
-
// ...
190
-
}
191
-
```
192
-
193
185
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.
194
186
195
187
#### New projects {/*new-projects*/}
196
188
197
189
If you're starting a new project, you can enable the compiler on your entire codebase, which is the default behavior.
198
190
199
-
##Usage {/*installation*/}
191
+
### Using React Compiler with React 17 or 18 {/*using-react-compiler-with-react-17-or-18*/}
200
192
201
-
### Babel {/*usage-with-babel*/}
193
+
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.
`babel-plugin-react-compiler` should run first before other Babel plugins as the compiler requires the input source information for sound analysis.
216
+
### Using the compiler on libraries {/*using-the-compiler-on-libraries*/}
226
217
227
-
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.
218
+
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.
219
+
220
+
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.
221
+
222
+
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).
223
+
224
+
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.
225
+
226
+
## Usage {/*installation*/}
227
+
228
+
### Babel {/*usage-with-babel*/}
228
229
229
230
<TerminalBlock>
230
-
npm install react-compiler-runtime@experimental
231
+
npm install babel-plugin-react-compiler@beta
231
232
</TerminalBlock>
232
233
233
-
You should also add the correct `target` to your compiler config, where `target` is the major version of React you are targeting:
234
+
The compiler includes a Babel plugin which you can use in your build pipeline to run the compiler.
234
235
235
-
```js {3}
236
+
After installing, add it to your Babel config. Please note that it's critical that the compiler run **first** in the pipeline:
A community Webpack loader is [now available here](https://github.com/SukkaW/react-compiler-webpack).
372
314
373
315
### Expo {/*usage-with-expo*/}
374
316
@@ -392,11 +334,15 @@ To report issues, please first create a minimal repro on the [React Compiler Pla
392
334
393
335
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).
394
336
395
-
### `(0 , _c) is not a function` error {/*0--_c-is-not-a-function-error*/}
337
+
### What does the compiler assume? {/*what-does-the-compiler-assume*/}
338
+
339
+
React Compiler assumes that your code:
396
340
397
-
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.
341
+
1. Is valid, semantic JavaScript.
342
+
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`.
343
+
3. Follows the [Rules of React](https://react.dev/reference/rules).
398
344
399
-
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.
345
+
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).
400
346
401
347
### How do I know my components have been optimized? {/*how-do-i-know-my-components-have-been-optimized*/}
Copy file name to clipboardexpand all lines: src/content/reference/react/useReducer.md
+1-1
Original file line number
Diff line number
Diff line change
@@ -52,7 +52,7 @@ function MyComponent() {
52
52
#### Caveats {/*caveats*/}
53
53
54
54
* `useReducer` is a Hook, so you can only call it **at the top level of your component** or your own Hooks. You can't call it inside loops or conditions. If you need that, extract a new component and move the state into it.
55
-
* The `dispatch` function has a stable identity, so you will often see it omitted from effect dependencies, but including it will not cause the effect to fire. If the linter lets you omit a dependency without errors, it is safe to do. [Learn more about removing Effect dependencies.](/learn/removing-effect-dependencies#move-dynamic-objects-and-functions-inside-your-effect)
55
+
* The `dispatch` function has a stable identity, so you will often see it omitted from Effect dependencies, but including it will not cause the Effect to fire. If the linter lets you omit a dependency without errors, it is safe to do. [Learn more about removing Effect dependencies.](/learn/removing-effect-dependencies#move-dynamic-objects-and-functions-inside-your-effect)
56
56
* In Strict Mode, React will **call your reducer and initializer twice** in order to [help you find accidental impurities.](#my-reducer-or-initializer-function-runs-twice) This is development-only behavior and does not affect production. If your reducer and initializer are pure (as they should be), this should not affect your logic. The result from one of the calls is ignored.
Copy file name to clipboardexpand all lines: src/content/reference/react/useState.md
+1-1
Original file line number
Diff line number
Diff line change
@@ -85,7 +85,7 @@ function handleClick() {
85
85
86
86
* React [batches state updates.](/learn/queueing-a-series-of-state-updates) It updates the screen **after all the event handlers have run** and have called their `set` functions. This prevents multiple re-renders during a single event. In the rare case that you need to force React to update the screen earlier, for example to access the DOM, you can use [`flushSync`.](/reference/react-dom/flushSync)
87
87
88
-
* The `set` function has a stable identity, so you will often see it omitted from effect dependencies, but including it will not cause the effect to fire. If the linter lets you omit a dependency without errors, it is safe to do. [Learn more about removing Effect dependencies.](/learn/removing-effect-dependencies#move-dynamic-objects-and-functions-inside-your-effect)
88
+
* The `set` function has a stable identity, so you will often see it omitted from Effect dependencies, but including it will not cause the Effect to fire. If the linter lets you omit a dependency without errors, it is safe to do. [Learn more about removing Effect dependencies.](/learn/removing-effect-dependencies#move-dynamic-objects-and-functions-inside-your-effect)
89
89
90
90
* Calling the `set` function *during rendering* is only allowed from within the currently rendering component. React will discard its output and immediately attempt to render it again with the new state. This pattern is rarely needed, but you can use it to **store information from the previous renders**. [See an example below.](#storing-information-from-previous-renders)
Copy file name to clipboardexpand all lines: src/content/reference/react/useTransition.md
+1-1
Original file line number
Diff line number
Diff line change
@@ -80,7 +80,7 @@ function TabContainer() {
80
80
81
81
* The function you pass to `startTransition` must be synchronous. React immediately executes this function, marking all state updates that happen while it executes as Transitions. If you try to perform more state updates later (for example, in a timeout), they won't be marked as Transitions.
82
82
83
-
* The `startTransition` function has a stable identity, so you will often see it omitted from effect dependencies, but including it will not cause the effect to fire. If the linter lets you omit a dependency without errors, it is safe to do. [Learn more about removing Effect dependencies.](/learn/removing-effect-dependencies#move-dynamic-objects-and-functions-inside-your-effect)
83
+
* The `startTransition` function has a stable identity, so you will often see it omitted from Effect dependencies, but including it will not cause the Effect to fire. If the linter lets you omit a dependency without errors, it is safe to do. [Learn more about removing Effect dependencies.](/learn/removing-effect-dependencies#move-dynamic-objects-and-functions-inside-your-effect)
84
84
85
85
* A state update marked as a Transition will be interrupted by other state updates. For example, if you update a chart component inside a Transition, but then start typing into an input while the chart is in the middle of a re-render, React will restart the rendering work on the chart component after handling the input update.
0 commit comments