11---
2- title : Directives
2+ title : 指令
33---
44
55<Intro >
6- React Compiler directives are special string literals that control whether specific functions are compiled.
6+ React 编译器指令是特殊的字符串文字,用于控制特定函数是否被编译。
77</Intro >
88
99``` js
1010function MyComponent () {
11- " use memo" ; // Opt this component into compilation
11+ " use memo" ; // 选择让该组件加入编译
1212 return < div> {/* ... */ }< / div> ;
1313}
1414```
@@ -17,86 +17,86 @@ function MyComponent() {
1717
1818---
1919
20- ## Overview {/* overview* /}
20+ ## 概览 {/* overview* /}
2121
22- React Compiler directives provide fine-grained control over which functions are optimized by the compiler. They are string literals placed at the beginning of a function body or at the top of a module.
22+ React 编译器指令让你能够精细化地控制哪些函数由编译器进行优化。它们是放置在函数体开头或模块顶部的字符串文字。
2323
24- ### Available directives {/* available-directives* /}
24+ ### 可用的指令 {/* available-directives* /}
2525
26- * ** [ ` "use memo" ` ] ( /reference/react-compiler/directives/use-memo ) ** - Opts a function into compilation
27- * ** [ ` "use no memo" ` ] ( /reference/react-compiler/directives/use-no-memo ) ** - Opts a function out of compilation
26+ * ** [ ` "use memo" ` ] ( /reference/react-compiler/directives/use-memo ) ** - 让一个函数选择加入编译
27+ * ** [ ` "use no memo" ` ] ( /reference/react-compiler/directives/use-no-memo ) ** - 让一个函数选择退出编译
28+
29+ ### 快速比较 {/* quick-comparison* /}
2830
29- ### Quick comparison {/* quick-comparison* /}
30-
31- | Directive | Purpose | When to use |
31+ | 指令 | 目的 | 使用场景 |
3232| -----------| ---------| -------------|
33- | [ ` "use memo" ` ] ( /reference/react-compiler/directives/use-memo ) | Force compilation | When using ` annotation ` mode or to override ` infer ` mode heuristics |
34- | [ ` "use no memo" ` ] ( /reference/react-compiler/directives/use-no-memo ) | Prevent compilation | Debugging issues or working with incompatible code |
33+ | [ ` "use memo" ` ] ( /reference/react-compiler/directives/use-memo ) | 强制编译 | 当使用 ` annotation ` 模式时,或需要覆盖 ` infer ` 模式的推断逻辑时 |
34+ | [ ` "use no memo" ` ] ( /reference/react-compiler/directives/use-no-memo ) | 阻止编译 | 调试问题或处理不兼容的代码时 |
3535
3636---
3737
38- ## Usage {/* usage* /}
38+ ## 用法 {/* usage* /}
3939
40- ### Function-level directives {/* function-level* /}
40+ ### 函数级别的指令 {/* function-level* /}
4141
42- Place directives at the beginning of a function to control its compilation:
42+ 将指令放置在函数体的最开始,以控制该函数的编译行为:
4343
4444``` js
45- // Opt into compilation
45+ // 选择加入编译
4646function OptimizedComponent () {
4747 " use memo" ;
48- return < div> This will be optimized < / div> ;
48+ return < div> 这个组件将被优化 < / div> ;
4949}
5050
51- // Opt out of compilation
51+ // 选择退出编译
5252function UnoptimizedComponent () {
5353 " use no memo" ;
54- return < div> This won ' t be optimized </div>;
54+ return < div> 这个组件不会被优化 < / div> ;
5555}
5656```
5757
58- ### Module-level directives {/*module-level*/}
58+ ### 模块级别的指令 {/* module-level* /}
5959
60- Place directives at the top of a file to affect all functions in that module:
60+ 将指令放置在文件的顶部,以影响该模块中的所有函数:
6161
6262``` js
63- // At the very top of the file
63+ // 在文件的最顶部
6464" use memo" ;
6565
66- // All functions in this file will be compiled
66+ // 该文件中的所有函数都将被编译
6767function Component1 () {
68- return <div>Compiled </div>;
68+ return < div> 已编译 < / div> ;
6969}
7070
7171function Component2 () {
72- return <div>Also compiled </div>;
72+ return < div> 同样已编译 < / div> ;
7373}
7474
75- // Can be overridden at function level
75+ // 可以在函数级别被覆盖
7676function Component3 () {
77- "use no memo"; // This overrides the module directive
78- return <div>Not compiled </div>;
77+ " use no memo" ; // 这会覆盖模块指令
78+ return < div> 未编译 < / div> ;
7979}
8080```
8181
82- ### Compilation modes interaction {/*compilation-modes*/}
82+ ### 与编译模式的交互 {/* compilation-modes* /}
8383
84- Directives behave differently depending on your [`compilationMode`](/reference/react-compiler/compilationMode):
84+ 指令的行为因你的 [ ` compilationMode ` ] ( /reference/react-compiler/compilationMode ) 配置而异:
8585
86- * **`annotation` mode **: Only functions with `"use memo"` are compiled
87- * **`infer` mode **: Compiler decides what to compile, directives override decisions
88- * **`all` mode **: Everything is compiled, `"use no memo"` can exclude specific functions
86+ * ** ` annotation ` 模式 ** : 仅有带 ` "use memo" ` 的函数会被编译
87+ * ** ` infer ` 模式 ** : 编译器自行决定编译哪些内容,指令可以覆盖编译器的决定
88+ * ** ` all ` 模式 ** : 所有内容都会被编译, ` "use no memo" ` 可以用来排除特定的函数
8989
9090---
9191
92- ## Best practices {/*best-practices*/}
92+ ## 最佳实践 {/* best-practices* /}
9393
94- ### Use directives sparingly {/*use-sparingly*/}
94+ ### 谨慎使用指令 {/* use-sparingly* /}
9595
96- Directives are escape hatches. Prefer configuring the compiler at the project level:
96+ 指令是一种脱围机制 ( escape hatch)。应优先考虑在项目级别进行编译器配置:
9797
9898``` js
99- // ✅ Good - project-wide configuration
99+ // ✅ Good - 项目的全局配置
100100{
101101 plugins: [
102102 [' babel-plugin-react-compiler' , {
@@ -105,94 +105,94 @@ Directives are escape hatches. Prefer configuring the compiler at the project le
105105 ]
106106}
107107
108- // ⚠️ Use directives only when needed
108+ // ⚠️ 仅在必要时使用指令
109109function SpecialCase () {
110- "use no memo"; // Document why this is needed
110+ " use no memo" ; // 务必注释说明为何需要这样做
111111 // ...
112112}
113113```
114114
115- ### Document directive usage {/*document-usage*/}
115+ ### 为指令的使用添加文档说明 {/* document-usage* /}
116116
117- Always explain why a directive is used:
117+ 务必解释为何要使用某个指令:
118118
119119``` js
120- // ✅ Good - clear explanation
120+ // ✅ 推荐 - 清晰的解释
121121function DataGrid () {
122- "use no memo"; // TODO: Remove after fixing issue with dynamic row heights (JIRA-123)
123- // Complex grid implementation
122+ " use no memo" ; // TODO:修复动态行高问题后移除 (JIRA-123)
123+ // 复杂的表格实现
124124}
125125
126- // ❌ Bad - no explanation
126+ // ❌ 不推荐 - 没有解释
127127function Mystery () {
128128 " use no memo" ;
129129 // ...
130130}
131131```
132132
133- ### Plan for removal {/*plan-removal*/}
133+ ### 为移除指令制定计划 {/* plan-removal* /}
134134
135- Opt-out directives should be temporary:
135+ 选择退出编译的指令应该是临时性的:
136136
137- 1. Add the directive with a TODO comment
138- 2. Create a tracking issue
139- 3. Fix the underlying problem
140- 4. Remove the directive
137+ 1 . 添加指令,并附上 TODO 注释
138+ 2 . 创建一个跟踪此问题的事项
139+ 3 . 修复底层的问题
140+ 4 . 移除该指令
141141
142142``` js
143143function TemporaryWorkaround () {
144- "use no memo"; // TODO: Remove after upgrading ThirdPartyLib to v2.0
144+ " use no memo" ; // TODO: 待 ThirdPartyLib 升级到 v2.0 后移除
145145 return < ThirdPartyComponent / > ;
146146}
147147```
148148
149149---
150150
151- ## Common patterns {/*common-patterns*/}
151+ ## 常见模式 {/* common-patterns* /}
152152
153- ### Gradual adoption {/*gradual-adoption*/}
153+ ### 渐进式引入 {/* gradual-adoption* /}
154154
155- When adopting the React Compiler in a large codebase:
155+ 在大型代码库中引入 React 编译器时:
156156
157157``` js
158- // Start with annotation mode
158+ // 从 annotation 模式开始
159159{
160160 compilationMode: ' annotation'
161161}
162162
163- // Opt in stable components
163+ // 将稳定的组件加入编译
164164function StableComponent () {
165165 " use memo" ;
166- // Well-tested component
166+ // 经过充分测试的组件
167167}
168168
169- // Later, switch to infer mode and opt out problematic ones
169+ // 后续可以切换到 infer 模式,并将有问题的组件排除掉
170170function ProblematicComponent () {
171- "use no memo"; // Fix issues before removing
171+ " use no memo" ; // 在移除此指令前先修复相关问题
172172 // ...
173173}
174174```
175175
176176
177177---
178178
179- ## Troubleshooting {/*troubleshooting*/}
179+ ## 故障排除 {/* troubleshooting* /}
180180
181- For specific issues with directives, see the troubleshooting sections in:
181+ 关于指令的具体问题,请参阅以下页面的问题排查部分:
182182
183- * [`"use memo"` troubleshooting ](/reference/react-compiler/directives/use-memo#troubleshooting)
184- * [`"use no memo"` troubleshooting ](/reference/react-compiler/directives/use-no-memo#troubleshooting)
183+ * [ ` "use memo" ` 问题排查 ] ( /reference/react-compiler/directives/use-memo#troubleshooting )
184+ * [ ` "use no memo" ` 问题排查 ] ( /reference/react-compiler/directives/use-no-memo#troubleshooting )
185185
186- ### Common issues {/*common-issues*/}
186+ ### 常见问题 {/* common-issues* /}
187187
188- 1. **Directive ignored **: Check placement (must be first) and spelling
189- 2. **Compilation still happens **: Check `ignoreUseNoForget` setting
190- 3. **Module directive not working **: Ensure it ' s before all imports
188+ 1 . ** 指令被忽略 ** : 检查指令的位置(必须在最前面)和拼写
189+ 2 . ** 代码仍然被编译 ** : 检查 ` ignoreUseNoForget ` 配置项
190+ 3 . ** 模块级指令不生效 ** : 确保它在所有 ` import ` 语句之前
191191
192192---
193193
194- ## See also {/* see-also*/ }
194+ ## 另请参阅 {/* see-also* /}
195195
196- * [` compilationMode` ](/ reference/ react- compiler/ compilationMode) - Configure how the compiler chooses what to optimize
197- * [` Configuration` ](/ reference/ react- compiler/ configuration) - Full compiler configuration options
198- * [React Compiler documentation ](https: // react.dev/learn/react-compiler) - Getting started guide
196+ * [ ` compilationMode ` ] ( /reference/react-compiler/compilationMode ) - 配置编译器如何选择要优化的内容
197+ * [ ` Configuration ` ] ( /reference/react-compiler/configuration ) - 完整的编译器配置选项
198+ * [ React 编译器文档 ] ( https://react.dev/learn/react-compiler ) - 入门指南
0 commit comments