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 memo"`marks a function for React Compiler optimization.
8
+
`"use memo"`用于标记一个函数,以便 React 编译器对其进行优化。
9
9
10
10
</Intro>
11
11
12
12
<Note>
13
13
14
-
In most cases, you don't need `"use memo"`. It's primarily needed in `annotation`mode where you must explicitly mark functions for optimization. In `infer`mode, the compiler automatically detects components and hooks by their naming patterns (PascalCase for components, `use` prefix for hooks). If a component or hook isn't being compiled in `infer`mode, you should fix its naming convention rather than forcing compilation with `"use memo"`.
Add`"use memo"` at the beginning of a function to mark it for React Compiler optimization.
26
+
在函数的开头添加`"use memo"`,以此来标记该函数,使其可以被 React 编译器优化。
27
27
28
28
```js {1}
29
29
functionMyComponent() {
@@ -32,59 +32,59 @@ function MyComponent() {
32
32
}
33
33
```
34
34
35
-
When a function contains `"use memo"`, the React Compiler will analyze and optimize it during build time. The compiler will automatically memoize values and components to prevent unnecessary re-computations and re-renders.
In a React app that uses the React Compiler, functions are analyzed at build time to determine if they can be optimized. By default, the compiler automatically infers which components to memoize, but this can depend on your [`compilationMode`](/reference/react-compiler/compilationMode) setting if you've set it.
`"use memo"`explicitly marks a function for optimization, overriding the default behavior:
49
+
`"use memo"`可以显式地标记一个函数需要被优化,从而覆盖编译器的默认行为:
50
50
51
-
*In`annotation`mode: Only functions with `"use memo"`are optimized
52
-
*In`infer`mode: The compiler uses heuristics, but `"use memo"`forces optimization
53
-
*In`all`mode: Everything is optimized by default, making `"use memo"`redundant
51
+
*在`annotation`模式下:只有带 `"use memo"`的函数才会被优化
52
+
*在`infer`模式下:编译器使用启发式规则进行推断,但 `"use memo"`会强制进行优化
53
+
*在`all`模式下:所有内容默认都会被优化,这使得 `"use memo"`成为多余的
54
54
55
-
The directive creates a clear boundary in your codebase between optimized and non-optimized code, giving you fine-grained control over the compilation process.
55
+
该指令在你的代码库中为优化和非优化的代码创建了一个清晰的界限,让你能对编译过程进行精细化控制。
56
56
57
-
### When to use`"use memo"` {/*when-to-use*/}
57
+
### 何时使用`"use memo"` {/*when-to-use*/}
58
58
59
-
You should consider using `"use memo"` when:
59
+
你应该在以下情况考虑使用 `"use memo"`:
60
60
61
-
#### You're using annotation mode {/*annotation-mode-use*/}
62
-
In`compilationMode: 'annotation'`, the directive is required for any function you want optimized:
//Automatically memoized because this is named like a Component
129
+
//会被自动记忆化,因为它的命名符合组件规范
130
130
functionComplexDashboard({ data }) {
131
131
// ...
132
132
}
133
133
134
-
//Skipped: Is not named like a Component
134
+
//会被跳过:因为它的命名不符合组件规范
135
135
functionsimpleDisplay({ text }) {
136
136
// ...
137
137
}
138
138
```
139
139
140
-
In`infer`mode, the compiler automatically detects components and hooks by their naming patterns (PascalCase for components,`use`prefix for hooks). If a component or hook isn't being compiled in `infer`mode, you should fix its naming convention rather than forcing compilation with `"use memo"`.
0 commit comments