Skip to content

Commit 66f99a9

Browse files
authored
docs(cn): Translate 'use memo' directive documentation to Chinese (#1827)
2 parents 495264a + 652ca7b commit 66f99a9

File tree

1 file changed

+49
-49
lines changed
  • src/content/reference/react-compiler/directives

1 file changed

+49
-49
lines changed
Lines changed: 49 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
---
22
title: "use memo"
3-
titleForTitleTag: "'use memo' directive"
3+
titleForTitleTag: "'use memo' 指令"
44
---
55

66
<Intro>
77

8-
`"use memo"` marks a function for React Compiler optimization.
8+
`"use memo"` 用于标记一个函数,以便 React 编译器对其进行优化。
99

1010
</Intro>
1111

1212
<Note>
1313

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"`.
14+
在大多数情况下,你并不需要使用 `"use memo"`。它主要用于 `annotation` 模式,在该模式下你必须显式地标记需要优化的函数。在 `infer` 模式下,编译器会根据命名约定(组件使用 PascalCase 命名法,Hooks 使用 use 前缀)自动检测组件和 hook。如果在 `infer` 模式下某个组件或 hook 没有被编译,你应该修正它的命名,而不是用 `"use memo"` 来强制编译。
1515

1616
</Note>
1717

1818
<InlineToc />
1919

2020
---
2121

22-
## Reference {/*reference*/}
22+
## 参考 {/*reference*/}
2323

2424
### `"use memo"` {/*use-memo*/}
2525

26-
Add `"use memo"` at the beginning of a function to mark it for React Compiler optimization.
26+
在函数的开头添加 `"use memo"`,以此来标记该函数,使其可以被 React 编译器优化。
2727

2828
```js {1}
2929
function MyComponent() {
@@ -32,59 +32,59 @@ function MyComponent() {
3232
}
3333
```
3434

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.
35+
当一个函数包含 `"use memo"` 指令时,React 编译器将在构建时对其进行分析和优化。编译器会自动对值和组件进行记忆化,以防止不必要的重计算和重渲染。
3636

37-
#### Caveats {/*caveats*/}
37+
#### 注意事项 {/*caveats*/}
3838

39-
* `"use memo"` must be at the very beginning of a function body, before any imports or other code (comments are OK).
40-
* The directive must be written with double or single quotes, not backticks.
41-
* The directive must exactly match `"use memo"`.
42-
* Only the first directive in a function is processed; additional directives are ignored.
43-
* The effect of the directive depends on your [`compilationMode`](/reference/react-compiler/compilationMode) setting.
39+
* `"use memo"` 必须位于函数体的最开始,在任何 import 语句或其他代码之前(注释除外)。
40+
* 该指令必须使用双引号或单引号,而不是反引号。
41+
* 该指令必须与 `"use memo"` 完全匹配。
42+
* 只有一个函数中的第一个指令会被处理;其余的指令将被忽略。
43+
* 指令的效果取决于你的 [`compilationMode`](/reference/react-compiler/compilationMode) 配置。
4444

45-
### How `"use memo"` marks functions for optimization {/*how-use-memo-marks*/}
45+
### `"use memo"` 如何标记函数以进行优化 {/*how-use-memo-marks*/}
4646

47-
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.
47+
在使用 React 编译器的应用中,所有函数都会在构建时被分析,以确定它们是否可以被优化。默认情况下,编译器会自动推断哪些组件需要被记忆化,但这取决于你所设置的 [`compilationMode`](/reference/react-compiler/compilationMode)
4848

49-
`"use memo"` explicitly marks a function for optimization, overriding the default behavior:
49+
`"use memo"` 可以显式地标记一个函数需要被优化,从而覆盖编译器的默认行为:
5050

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"` 成为多余的
5454

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+
该指令在你的代码库中为优化和非优化的代码创建了一个清晰的界限,让你能对编译过程进行精细化控制。
5656

57-
### When to use `"use memo"` {/*when-to-use*/}
57+
### 何时使用 `"use memo"` {/*when-to-use*/}
5858

59-
You should consider using `"use memo"` when:
59+
你应该在以下情况考虑使用 `"use memo"`:
6060

61-
#### You're using annotation mode {/*annotation-mode-use*/}
62-
In `compilationMode: 'annotation'`, the directive is required for any function you want optimized:
61+
#### 你使用 annotation 模式 {/*annotation-mode-use*/}
62+
`compilationMode: 'annotation'` 时,任何你希望被优化的函数都必须使用该指令:
6363

6464
```js
65-
//This component will be optimized
65+
//这个组件将会被优化
6666
function OptimizedList() {
6767
"use memo";
6868
// ...
6969
}
7070

71-
//This component won't be optimized
71+
//这个组件不会被优化
7272
function SimpleWrapper() {
7373
// ...
7474
}
7575
```
7676

77-
#### You're gradually adopting React Compiler {/*gradual-adoption*/}
78-
Start with `annotation` mode and selectively optimize stable components:
77+
#### 你正在渐进式地引入 React 编译器 {/*gradual-adoption*/}
78+
可以从 `annotation` 模式开始,并有选择性地优化那些稳定的组件:
7979

8080
```js
81-
// Start by optimizing leaf components
81+
// 从优化叶子组件开始
8282
function Button({ onClick, children }) {
8383
"use memo";
8484
// ...
8585
}
8686

87-
// Gradually move up the tree as you verify behavior
87+
// 在验证其行为后,逐步向组件树上层移动
8888
function ButtonGroup({ buttons }) {
8989
"use memo";
9090
// ...
@@ -93,65 +93,65 @@ function ButtonGroup({ buttons }) {
9393

9494
---
9595

96-
## Usage {/*usage*/}
96+
## 用法 {/*usage*/}
9797

98-
### Working with different compilation modes {/*compilation-modes*/}
98+
### 在不同编译模式下的使用 {/*compilation-modes*/}
9999

100-
The behavior of `"use memo"` changes based on your compiler configuration:
100+
`"use memo"` 的行为会根据你的编译器配置而改变:
101101

102102
```js
103103
// babel.config.js
104104
module.exports = {
105105
plugins: [
106106
['babel-plugin-react-compiler', {
107-
compilationMode: 'annotation' // or 'infer' or 'all'
107+
compilationMode: 'annotation' // 'infer' 'all'
108108
}]
109109
]
110110
};
111111
```
112112

113-
#### Annotation mode {/*annotation-mode-example*/}
113+
#### Annotation 模式 {/*annotation-mode-example*/}
114114
```js
115-
//Optimized with "use memo"
115+
//使用 "use memo" 进行优化
116116
function ProductCard({ product }) {
117117
"use memo";
118118
// ...
119119
}
120120

121-
//Not optimized (no directive)
121+
//未优化 (没有指令)
122122
function ProductList({ products }) {
123123
// ...
124124
}
125125
```
126126

127-
#### Infer mode (default) {/*infer-mode-example*/}
127+
#### Infer 模式(默认){/*infer-mode-example*/}
128128
```js
129-
// Automatically memoized because this is named like a Component
129+
// 会被自动记忆化,因为它的命名符合组件规范
130130
function ComplexDashboard({ data }) {
131131
// ...
132132
}
133133

134-
// Skipped: Is not named like a Component
134+
// 会被跳过:因为它的命名不符合组件规范
135135
function simpleDisplay({ text }) {
136136
// ...
137137
}
138138
```
139139

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"`.
140+
`infer` 模式下,编译器会根据命名约定(组件使用 PascalCase 命名法,hook 使用 `use` 前缀)自动检测组件和 hook。如果在 `infer` 模式下某个组件或 hook 没有被编译,你应该修正它的命名,而不是用 `"use memo"` 来强制编译。
141141

142142
---
143143

144-
## Troubleshooting {/*troubleshooting*/}
144+
## 故障排除 {/*troubleshooting*/}
145145

146-
### Verifying optimization {/*verifying-optimization*/}
146+
### 验证优化效果 {/*verifying-optimization*/}
147147

148-
To confirm your component is being optimized:
148+
要确认你的组件是否被成功优化,可以:
149149

150-
1. Check the compiled output in your build
151-
2. Use React DevTools to check for Memo ✨ badge
150+
1. 检查你构建产物中被编译后的输出代码
151+
2. 使用 React 开发工具检查组件是否带有 Memo ✨ 徽章
152152

153-
### See also {/*see-also*/}
153+
### 参见 {/*see-also*/}
154154

155-
* [`"use no memo"`](/reference/react-compiler/directives/use-no-memo) - Opt out of compilation
156-
* [`compilationMode`](/reference/react-compiler/compilationMode) - Configure compilation behavior
157-
* [React Compiler](/learn/react-compiler) - Getting started guide
155+
* [`"use no memo"`](/reference/react-compiler/directives/use-no-memo) - 选择退出编译
156+
* [`compilationMode`](/reference/react-compiler/compilationMode) - 配置编译行为
157+
* [React 编译器](/learn/react-compiler) - 入门指南

0 commit comments

Comments
 (0)