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
`"use no memo"`prevents a function from being optimized by React Compiler.
8
+
`"use no memo"`可以防止函数被 React 编译器优化。
9
9
10
10
</Intro>
11
11
12
12
<InlineToc />
13
13
14
14
---
15
15
16
-
## Reference {/*reference*/}
16
+
## 参考 {/*reference*/}
17
17
18
18
### `"use no memo"` {/*use-no-memo*/}
19
19
20
-
Add`"use no memo"`at the beginning of a function to prevent React Compiler optimization.
20
+
在函数的开头添加`"use no memo"`可以阻止 React 编译器进行优化。
21
21
22
22
```js {1}
23
23
functionMyComponent() {
@@ -26,122 +26,122 @@ function MyComponent() {
26
26
}
27
27
```
28
28
29
-
When a function contains `"use no memo"`, the React Compiler will skip it entirely during optimization. This is useful as a temporary escape hatch when debugging or when dealing with code that doesn't work correctly with the compiler.
29
+
当一个函数包含 "use no memo" 时,React 编译器会在优化过程中完全跳过它。在调试或处理与编译器不兼容的代码时,这是一个很有用的脱围机制。
30
30
31
-
#### Caveats {/*caveats*/}
31
+
#### 注意事项 {/*caveats*/}
32
32
33
-
*`"use no memo"`must be at the very beginning of a function body, before any imports or other code (comments are OK).
34
-
*The directive must be written with double or single quotes, not backticks.
35
-
*The directive must exactly match `"use no memo"`or its alias `"use no forget"`.
36
-
*This directive takes precedence over all compilation modes and other directives.
37
-
*It's intended as a temporary debugging tool, not a permanent solution.
33
+
*`"use no memo"`必须位于函数体的最开始,在任何导入或其他代码之前(注释可以)。
34
+
*该指令必须使用双引号或单引号,而不是反引号。
35
+
*该指令必须与 `"use no memo"`或其别名 `"use no forget"` 完全匹配。
36
+
*该指令的优先级高于所有编译模式和其他指令。
37
+
*它旨在作为一种临时的调试工具,而非永久的解决方案。
38
38
39
-
### How `"use no memo"`opts-out of optimization {/*how-use-no-memo-opts-out*/}
39
+
### `"use no memo"`如何选择退出优化 {/*how-use-no-memo-opts-out*/}
40
40
41
-
React Compiler analyzes your code at build time to apply optimizations. `"use no memo"`creates an explicit boundary that tells the compiler to skip a function entirely.
41
+
React 编译器会在构建时分析你的代码以应用优化。`"use no memo"`创建了一个明确的边界,告诉编译器完全跳过一个函数。
42
42
43
-
This directive takes precedence over all other settings:
44
-
*In`all`mode: The function is skipped despite the global setting
45
-
*In`infer`mode: The function is skipped even if heuristics would optimize it
43
+
该指令的优先级高于所有其他设置:
44
+
*在`all`模式下:尽管有全局设置,该函数仍会被跳过
45
+
*在`infer`模式下:即使启发式算法会优化该函数,它也仍会被跳过
46
46
47
-
The compiler treats these functions as if the React Compiler wasn't enabled, leaving them exactly as written.
47
+
编译器会像未启用 React 编译器一样对待这些函数,让它们保持原样。
48
48
49
-
### When to use`"use no memo"` {/*when-to-use*/}
49
+
### 何时使用`"use no memo"` {/*when-to-use*/}
50
50
51
-
`"use no memo"`should be used sparingly and temporarily. Common scenarios include:
0 commit comments