Skip to content

Commit aa44c96

Browse files
authored
Update react-compiler.md
1 parent 1e782ec commit aa44c96

File tree

1 file changed

+36
-105
lines changed

1 file changed

+36
-105
lines changed

src/content/learn/react-compiler.md

Lines changed: 36 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ These docs are still a work in progress. More documentation is available in the
1313
<YouWillLearn>
1414

1515
* Getting started with the compiler
16-
* Installing the compiler and eslint plugin
16+
* Installing the compiler and ESLint plugin
1717
* Troubleshooting
1818

1919
</YouWillLearn>
@@ -26,7 +26,7 @@ The latest Beta release can be found with the `@beta` tag, and daily experimenta
2626

2727
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.
2828

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.
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.
3030

3131
The compiler is currently released as `beta`, and is available to try out on React 17+ apps and libraries. To install the Beta:
3232

@@ -48,6 +48,10 @@ In order to optimize applications, React Compiler automatically memoizes your co
4848

4949
The compiler uses its knowledge of JavaScript and React's rules to automatically memoize values or groups of values within your components and hooks. If it detects breakages of the rules, it will automatically skip over just those components or hooks, and continue safely compiling other code.
5050

51+
<Note>
52+
React Compiler can statically detect when Rules of React are broken, and safely opt-out of optimizing just the affected components or hooks. It is not necessary for the compiler to optimize 100% of your codebase.
53+
</Note>
54+
5155
If your codebase is already very well-memoized, you might not expect to see major performance improvements with the compiler. However, in practice memoizing the correct dependencies that cause performance issues is tricky to get right by hand.
5256

5357
<DeepDive>
@@ -120,52 +124,49 @@ Please note that the compiler is still in Beta and has many rough edges. While i
120124

121125
In addition to these docs, we recommend checking the [React Compiler Working Group](https://github.com/reactwg/react-compiler) for additional information and discussion about the compiler.
122126

123-
### Checking compatibility {/*checking-compatibility*/}
124-
125-
Prior to installing the compiler, you can first check to see if your codebase is compatible:
126-
127-
<TerminalBlock>
128-
npx react-compiler-healthcheck@beta
129-
</TerminalBlock>
130-
131-
This script will:
132-
133-
- Check how many components can be successfully optimized: higher is better
134-
- Check for `<StrictMode>` usage: having this enabled and followed means a higher chance that the [Rules of React](/reference/rules) are followed
135-
- Check for incompatible library usage: known libraries that are incompatible with the compiler
136-
137-
As an example:
138-
139-
<TerminalBlock>
140-
Successfully compiled 8 out of 9 components.
141-
StrictMode usage not found.
142-
Found no usage of incompatible libraries.
143-
</TerminalBlock>
144-
145127
### Installing eslint-plugin-react-compiler {/*installing-eslint-plugin-react-compiler*/}
146128

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.
129+
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.
148130

149131
<TerminalBlock>
150132
npm install -D eslint-plugin-react-compiler@beta
151133
</TerminalBlock>
152134

153-
Then, add it to your eslint config:
135+
Then, add it to your ESLint config:
136+
137+
```js
138+
import reactCompiler from 'eslint-plugin-react-compiler'
139+
140+
export default [
141+
{
142+
plugins: {
143+
'react-compiler': reactCompiler,
144+
},
145+
rules: {
146+
'react-compiler/react-compiler': 'error',
147+
},
148+
},
149+
]
150+
```
151+
152+
Or, in the deprecated eslintrc config format:
154153

155154
```js
156155
module.exports = {
157156
plugins: [
158157
'eslint-plugin-react-compiler',
159158
],
160159
rules: {
161-
'react-compiler/react-compiler': "error",
160+
'react-compiler/react-compiler': 'error',
162161
},
163162
}
164163
```
165164

166-
The eslint plugin will display any violations of the rules of React in your editor. When it does this, it means that the compiler has skipped over optimizing that component or hook. This is perfectly okay, and the compiler can recover and continue optimizing other components in your codebase.
165+
The ESLint plugin will display any violations of the rules of React in your editor. When it does this, it means that the compiler has skipped over optimizing that component or hook. This is perfectly okay, and the compiler can recover and continue optimizing other components in your codebase.
167166

168-
**You don't have to fix all eslint violations straight away.** You can address them at your own pace to increase the amount of components and hooks being optimized, but it is not required to fix everything before you can use the compiler.
167+
<Note>
168+
**You don't have to fix all ESLint violations straight away.** You can address them at your own pace to increase the amount of components and hooks being optimized, but it is not required to fix everything before you can use the compiler.
169+
</Note>
169170

170171
### Rolling out the compiler to your codebase {/*using-the-compiler-effectively*/}
171172

@@ -223,76 +224,6 @@ Library code can often require more complex patterns and usage of escape hatches
223224

224225
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.
225226

226-
### Using React Compiler with React 17 or 18 {/*using-react-compiler-with-react-17-or-18*/}
227-
228-
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.
229-
230-
<TerminalBlock>
231-
npm install react-compiler-runtime@beta
232-
</TerminalBlock>
233-
234-
You should also add the correct `target` to your compiler config, where `target` is the major version of React you are targeting:
235-
236-
```js {3}
237-
// babel.config.js
238-
const ReactCompilerConfig = {
239-
target: '18' // '17' | '18' | '19'
240-
};
241-
242-
module.exports = function () {
243-
return {
244-
plugins: [
245-
['babel-plugin-react-compiler', ReactCompilerConfig],
246-
],
247-
};
248-
};
249-
```
250-
251-
### Using the compiler on libraries {/*using-the-compiler-on-libraries*/}
252-
253-
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.
254-
255-
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.
256-
257-
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).
258-
259-
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.
260-
261-
### Using React Compiler with React 17 or 18 {/*using-react-compiler-with-react-17-or-18*/}
262-
263-
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.
264-
265-
<TerminalBlock>
266-
npm install react-compiler-runtime@beta
267-
</TerminalBlock>
268-
269-
You should also add the correct `target` to your compiler config, where `target` is the major version of React you are targeting:
270-
271-
```js {3}
272-
// babel.config.js
273-
const ReactCompilerConfig = {
274-
target: '18' // '17' | '18' | '19'
275-
};
276-
277-
module.exports = function () {
278-
return {
279-
plugins: [
280-
['babel-plugin-react-compiler', ReactCompilerConfig],
281-
],
282-
};
283-
};
284-
```
285-
286-
### Using the compiler on libraries {/*using-the-compiler-on-libraries*/}
287-
288-
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.
289-
290-
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.
291-
292-
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).
293-
294-
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.
295-
296227
## Usage {/*installation*/}
297228

298229
### Babel {/*usage-with-babel*/}
@@ -347,7 +278,7 @@ export default defineConfig(() => {
347278

348279
### Next.js {/*usage-with-nextjs*/}
349280

350-
Please refer to the [Next.js docs](https://nextjs.org/docs/canary/app/api-reference/next-config-js/reactCompiler) for more information.
281+
Please refer to the [Next.js docs](https://nextjs.org/docs/app/api-reference/next-config-js/reactCompiler) for more information.
351282

352283
### Remix {/*usage-with-remix*/}
353284
Install `vite-plugin-babel`, and add the compiler's Babel plugin to it:
@@ -380,11 +311,11 @@ export default defineConfig({
380311

381312
### Webpack {/*usage-with-webpack*/}
382313

383-
A community Webpack loader is [now available here](https://github.com/SukkaW/react-compiler-webpack).
314+
A community webpack loader is [now available here](https://github.com/SukkaW/react-compiler-webpack).
384315

385316
### Expo {/*usage-with-expo*/}
386317

387-
Please refer to [Expo's docs](https://docs.expo.dev/preview/react-compiler/) to enable and use the React Compiler in Expo apps.
318+
Please refer to [Expo's docs](https://docs.expo.dev/guides/react-compiler/) to enable and use the React Compiler in Expo apps.
388319

389320
### Metro (React Native) {/*usage-with-react-native-metro*/}
390321

@@ -416,14 +347,14 @@ React Compiler can verify many of the Rules of React statically, and will safely
416347

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

419-
[React Devtools](/learn/react-developer-tools) (v5.0+) has built-in support for React Compiler and will display a "Memo ✨" badge next to components that have been optimized by the compiler.
350+
[React DevTools](/learn/react-developer-tools) (v5.0+) and [React Native DevTools](https://reactnative.dev/docs/react-native-devtools) have built-in support for React Compiler and will display a "Memo ✨" badge next to components that have been optimized by the compiler.
420351

421352
### Something is not working after compilation {/*something-is-not-working-after-compilation*/}
422-
If you have eslint-plugin-react-compiler installed, the compiler will display any violations of the rules of React in your editor. When it does this, it means that the compiler has skipped over optimizing that component or hook. This is perfectly okay, and the compiler can recover and continue optimizing other components in your codebase. **You don't have to fix all eslint violations straight away.** You can address them at your own pace to increase the amount of components and hooks being optimized.
353+
If you have eslint-plugin-react-compiler installed, the compiler will display any violations of the rules of React in your editor. When it does this, it means that the compiler has skipped over optimizing that component or hook. This is perfectly okay, and the compiler can recover and continue optimizing other components in your codebase. **You don't have to fix all ESLint violations straight away.** You can address them at your own pace to increase the amount of components and hooks being optimized.
423354

424355
Due to the flexible and dynamic nature of JavaScript however, it's not possible to comprehensively detect all cases. Bugs and undefined behavior such as infinite loops may occur in those cases.
425356

426-
If your app doesn't work properly after compilation and you aren't seeing any eslint errors, the compiler may be incorrectly compiling your code. To confirm this, try to make the issue go away by aggressively opting out any component or hook you think might be related via the [`"use no memo"` directive](#opt-out-of-the-compiler-for-a-component).
357+
If your app doesn't work properly after compilation and you aren't seeing any ESLint errors, the compiler may be incorrectly compiling your code. To confirm this, try to make the issue go away by aggressively opting out any component or hook you think might be related via the [`"use no memo"` directive](#opt-out-of-the-compiler-for-a-component).
427358

428359
```js {2}
429360
function SuspiciousComponent() {

0 commit comments

Comments
 (0)