@@ -4,109 +4,109 @@ title: compilationMode
44
55<Intro >
66
7- The ` compilationMode ` option controls how the React Compiler selects which functions to compile.
7+ ` compilationMode ` 选项控制 React 编译器如何选择要编译的函数。
88
99</Intro >
1010
1111``` js
1212{
13- compilationMode: ' infer' // or 'annotation', 'syntax', 'all'
13+ compilationMode: ' infer' // 或 'annotation'、 'syntax' 和 'all'
1414}
1515```
1616
1717<InlineToc />
1818
1919---
2020
21- ## Reference {/* reference* /}
21+ ## 参考 {/* reference* /}
2222
2323### ` compilationMode ` {/* compilationmode* /}
2424
25- Controls the strategy for determining which functions the React Compiler will optimize.
25+ 控制用于确定 React 编译器将优化哪些函数的策略。
2626
27- #### Type {/* type* /}
27+ #### 类型 {/* type* /}
2828
2929```
3030'infer' | 'syntax' | 'annotation' | 'all'
3131```
3232
33- #### Default value {/* default-value* /}
33+ #### 默认值 {/* default-value* /}
3434
3535` 'infer' `
3636
37- #### Options {/* options* /}
37+ #### 选项 {/* options* /}
3838
39- - ** ` 'infer' ` ** (default): The compiler uses intelligent heuristics to identify React components and hooks:
40- - Functions explicitly annotated with ` "use memo" ` directive
41- - Functions that are named like components ( PascalCase) or hooks ( ` use ` prefix) AND create JSX and/or call other hooks
39+ - ** ` 'infer' ` ** (默认值):编译器使用智能的启发式方法来识别 React 组件和 Hook:
40+ - 明确使用 ` "use memo" ` 指令注释的函数
41+ - 命名类似组件( PascalCase)或 Hook( ` use ` 前缀)并且创建了 JSX 和/或调用了其他 Hook 的函数
4242
43- - ** ` 'annotation' ` ** : Only compile functions explicitly marked with the ` "use memo" ` directive. Ideal for incremental adoption.
43+ - ** ` 'annotation' ` ** :仅编译使用 ` "use memo" ` 指示符明确标记的函数。是增量采用的理想选择。
4444
45- - ** ` 'syntax' ` ** : Only compile components and hooks that use Flow's [ component] ( https://flow.org/en/docs/react/component-syntax/ ) and [ hook] ( https://flow.org/en/docs/react/hook-syntax/ ) syntax.
45+ - ** ` 'syntax' ` ** :仅编译使用 Flow 的 [ component] ( https://flow.org/en/docs/react/component-syntax/ ) 和 [ hook] ( https://flow.org/en/docs/react/hook-syntax/ ) 语法的组件和 Hook。
4646
47- - ** ` 'all' ` ** : Compile all top-level functions. Not recommended as it may compile non- React functions.
47+ - ** ` 'all' ` ** :编译所有顶层函数。不推荐,因为它可能会编译非 React 函数。
4848
49- #### Caveats {/* caveats* /}
49+ #### 注意事项 {/* caveats* /}
5050
51- - The ` 'infer' ` mode requires functions to follow React naming conventions to be detected
52- - Using ` 'all' ` mode may negatively impact performance by compiling utility functions
53- - The ` 'syntax' ` mode requires Flow and won't work with TypeScript
54- - Regardless of mode, functions with ` "use no memo" ` directive are always skipped
51+ - ` 'infer' ` 模式要求函数遵循 React 的命名约定才能被检测到
52+ - 使用 ` 'all' ` 模式可能会因编译工具函数而对性能产生负面影响
53+ - ` 'syntax' ` 模式需要 Flow,无法与 TypeScript 一起使用
54+ - 无论在哪种模式下,带有 ` "use no memo" ` 指令的函数总会被跳过
5555
5656---
5757
58- ## Usage {/* usage* /}
58+ ## 用法 {/* usage* /}
5959
60- ### Default inference mode {/* default-inference-mode* /}
60+ ### 默认推断模式 {/* default-inference-mode* /}
6161
62- The default ` 'infer' ` mode works well for most codebases that follow React conventions:
62+ 默认的 ` 'infer' ` 模式适用于大多数遵循 React 约定的代码库:
6363
6464``` js
6565{
6666 compilationMode: ' infer'
6767}
6868```
6969
70- With this mode, these functions will be compiled:
70+ 在此模式下,以下函数将被编译:
7171
7272``` js
73- // ✅ Compiled: Named like a component + returns JSX
73+ // ✅ 已编译:命名类似组件 + 返回 JSX
7474function Button (props ) {
7575 return < button> {props .label }< / button> ;
7676}
7777
78- // ✅ Compiled: Named like a hook + calls hooks
78+ // ✅ 已编译:命名类似 Hook + 调用 Hook
7979function useCounter () {
8080 const [count , setCount ] = useState (0 );
8181 return [count, setCount];
8282}
8383
84- // ✅ Compiled: Explicit directive
84+ // ✅ 已编译:显式指令
8585function expensiveCalculation (data ) {
8686 " use memo" ;
8787 return data .reduce (/* ... */ );
8888}
8989
90- // ❌ Not compiled: Not a component/hook pattern
90+ // ❌ 未编译:不是组件/Hook 模式
9191function calculateTotal (items ) {
9292 return items .reduce ((a , b ) => a + b, 0 );
9393}
9494```
9595
96- ### Incremental adoption with annotation mode {/* incremental-adoption* /}
96+ ### 使用注解模式进行增量采用 {/* incremental-adoption* /}
9797
98- For gradual migration, use ` 'annotation' ` mode to only compile marked functions:
98+ 为了逐步迁移,可以使用 ` 'annotation' ` 模式,仅编译被标记的函数:
9999
100100``` js
101101{
102102 compilationMode: ' annotation'
103103}
104104```
105105
106- Then explicitly mark functions to compile:
106+ 然后显式地标记要编译的函数:
107107
108108``` js
109- // Only this function will be compiled
109+ // 只有这个函数会被编译
110110function ExpensiveList (props ) {
111111 " use memo" ;
112112 return (
@@ -118,51 +118,51 @@ function ExpensiveList(props) {
118118 );
119119}
120120
121- // This won't be compiled without the directive
121+ // 如果没有指令,这个函数将不会被编译
122122function NormalComponent (props ) {
123123 return < div> {props .content }< / div> ;
124124}
125125```
126126
127- ### Using Flow syntax mode {/* flow-syntax-mode* /}
127+ ### 使用 Flow 语法模式 {/* flow-syntax-mode* /}
128128
129- If your codebase uses Flow instead of TypeScript:
129+ 如果你的代码库使用 Flow 而不是 TypeScript:
130130
131131``` js
132132{
133133 compilationMode: ' syntax'
134134}
135135```
136136
137- Then use Flow's component syntax:
137+ 然后使用 Flow 的组件语法:
138138
139139``` js
140- // Compiled: Flow component syntax
140+ // 已编译: Flow 组件语法
141141component Button (label : string ) {
142142 return < button> {label}< / button> ;
143143}
144144
145- // Compiled: Flow hook syntax
145+ // 已编译: Flow hook 语法
146146hook useCounter (initial : number ) {
147147 const [count , setCount ] = useState (initial);
148148 return [count, setCount];
149149}
150150
151- // Not compiled: Regular function syntax
151+ // 未编译:常规函数语法
152152function helper (data ) {
153153 return process (data);
154154}
155155```
156156
157- ### Opting out specific functions {/* opting-out* /}
157+ ### 选择性地跳过特定函数 {/* opting-out* /}
158158
159- Regardless of compilation mode, use ` "use no memo" ` to skip compilation:
159+ 无论编译模式如何,都可以使用 ` "use no memo" ` 来跳过编译:
160160
161161``` js
162162function ComponentWithSideEffects () {
163- " use no memo" ; // Prevent compilation
163+ " use no memo" ; // 阻止编译
164164
165- // This component has side effects that shouldn't be memoized
165+ // 这个组件有不应被 memoize 的副作用
166166 logToAnalytics (' component_rendered' );
167167
168168 return < div> Content< / div> ;
@@ -171,29 +171,29 @@ function ComponentWithSideEffects() {
171171
172172---
173173
174- ## Troubleshooting {/* troubleshooting* /}
174+ ## 故障排除 {/* troubleshooting* /}
175175
176- ### Component not being compiled in infer mode {/* component-not-compiled-infer* /}
176+ ### 在 infer 模式下组件未被编译 {/* component-not-compiled-infer* /}
177177
178- In ` 'infer' ` mode, ensure your component follows React conventions:
178+ 在 ` 'infer' ` 模式下,请确保你的组件遵循 React 的约定:
179179
180180``` js
181- // ❌ Won't be compiled: lowercase name
181+ // ❌ 不会编译:小写名称
182182function button (props ) {
183183 return < button> {props .label }< / button> ;
184184}
185185
186- // ✅ Will be compiled: PascalCase name
186+ // ✅ 将会编译: PascalCase 名称
187187function Button (props ) {
188188 return < button> {props .label }< / button> ;
189189}
190190
191- // ❌ Won't be compiled: doesn't create JSX or call hooks
191+ // ❌ 不会编译:未创建 JSX 或调用 Hook
192192function useData () {
193193 return window .localStorage .getItem (' data' );
194194}
195195
196- // ✅ Will be compiled: calls a hook
196+ // ✅ 将会编译:调用了一个 Hook
197197function useData () {
198198 const [data ] = useState (() => window .localStorage .getItem (' data' ));
199199 return data;
0 commit comments