|
1 | 1 | ---
|
2 |
| -title: Rules of Hooks |
| 2 | +title: Hooks 规则 |
3 | 3 | ---
|
4 | 4 |
|
5 | 5 | <Intro>
|
6 |
| -Hooks are defined using JavaScript functions, but they represent a special type of reusable UI logic with restrictions on where they can be called. |
| 6 | +Hooks 是通过 JavaScript 函数定义的, 但它们代表了一种特殊的可重用 UI 逻辑,并且对它们的调用位置有限制。 |
7 | 7 | </Intro>
|
8 | 8 |
|
9 | 9 | <InlineToc />
|
10 | 10 |
|
11 | 11 | ---
|
12 | 12 |
|
13 |
| -## Only call Hooks at the top level {/*only-call-hooks-at-the-top-level*/} |
| 13 | +## 只在顶层调用 Hooks {/*only-call-hooks-at-the-top-level*/} |
14 | 14 |
|
15 |
| -Functions whose names start with `use` are called [*Hooks*](/reference/react) in React. |
| 15 | +名称以 `use` 开头的函数在 React 中被称为 [*Hooks*](/reference/react)。 |
16 | 16 |
|
17 |
| -**Don’t call Hooks inside loops, conditions, nested functions, or `try`/`catch`/`finally` blocks.** Instead, always use Hooks at the top level of your React function, before any early returns. You can only call Hooks while React is rendering a function component: |
| 17 | +**不要在循环、条件、嵌套函数或 `try`/`catch`/`finally` 块中调用 Hooks ** 相反,你应该在 React 函数的顶层使用 Hooks ,并且在任何提前返回之前。你只能在 React 函数组件中调用 Hooks : |
18 | 18 |
|
19 |
| -* ✅ Call them at the top level in the body of a [function component](/learn/your-first-component). |
20 |
| -* ✅ Call them at the top level in the body of a [custom Hook](/learn/reusing-logic-with-custom-hooks). |
| 19 | +* ✅ 在[函数组件](/learn/your-first-component)的顶层调用它们。 |
| 20 | +* ✅ 在[自定义 Hooks ](/learn/reusing-logic-with-custom-hooks)的顶层调用它们。 |
21 | 21 |
|
22 | 22 | ```js{2-3,8-9}
|
23 | 23 | function Counter() {
|
24 |
| - // ✅ Good: top-level in a function component |
| 24 | + // ✅ Good: 在函数组件顶层 |
25 | 25 | const [count, setCount] = useState(0);
|
26 | 26 | // ...
|
27 | 27 | }
|
28 | 28 |
|
29 | 29 | function useWindowWidth() {
|
30 |
| - // ✅ Good: top-level in a custom Hook |
| 30 | + // ✅ Good: 在自定义 Hooks 顶层 |
31 | 31 | const [width, setWidth] = useState(window.innerWidth);
|
32 | 32 | // ...
|
33 | 33 | }
|
34 | 34 | ```
|
35 | 35 |
|
36 |
| -It’s **not** supported to call Hooks (functions starting with `use`) in any other cases, for example: |
| 36 | +在其他任何情况下调用以 `use` 开头的 Hooks 是不支持的,例如: |
37 | 37 |
|
38 |
| -* 🔴 Do not call Hooks inside conditions or loops. |
39 |
| -* 🔴 Do not call Hooks after a conditional `return` statement. |
40 |
| -* 🔴 Do not call Hooks in event handlers. |
41 |
| -* 🔴 Do not call Hooks in class components. |
42 |
| -* 🔴 Do not call Hooks inside functions passed to `useMemo`, `useReducer`, or `useEffect`. |
43 |
| -* 🔴 Do not call Hooks inside `try`/`catch`/`finally` blocks. |
| 38 | +* 🔴 不要在条件或循环中调用 Hooks 。 |
| 39 | +* 🔴 不要在条件 `return` 语句之后调用 Hooks 。 |
| 40 | +* 🔴 不要在事件处理程序中调用 Hooks 。 |
| 41 | +* 🔴 不要在类组件中调用 Hooks 。 |
| 42 | +* 🔴 不要在传递给 `useMemo` 、`useReducer` 或 `useEffect` 的函数内部调用 Hooks 。 |
| 43 | +* 🔴 不要在 `try`/`catch`/`finally` 块中调用 Hooks 。 |
44 | 44 |
|
45 |
| -If you break these rules, you might see this error. |
| 45 | +如果你违反了这些规则,你可能会看到以下错误: |
46 | 46 |
|
47 | 47 | ```js{3-4,11-12,20-21}
|
48 | 48 | function Bad({ cond }) {
|
@@ -105,24 +105,24 @@ function Bad() {
|
105 | 105 | }
|
106 | 106 | ```
|
107 | 107 |
|
108 |
| -You can use the [`eslint-plugin-react-hooks` plugin](https://www.npmjs.com/package/eslint-plugin-react-hooks) to catch these mistakes. |
| 108 | +你可以使用 [`eslint-plugin-react-hooks` 插件](https://www.npmjs.com/package/eslint-plugin-react-hooks) 来捕捉这些错误。 |
109 | 109 |
|
110 | 110 | <Note>
|
111 | 111 |
|
112 |
| -[Custom Hooks](/learn/reusing-logic-with-custom-hooks) *may* call other Hooks (that's their whole purpose). This works because custom Hooks are also supposed to only be called while a function component is rendering. |
| 112 | +[自定义 Hooks](/learn/reusing-logic-with-custom-hooks) *可以* 调用其他 Hooks (这就是它们的目的)。因为自定义 Hooks 也只应该在函数组件渲染时被调用。 |
113 | 113 |
|
114 | 114 | </Note>
|
115 | 115 |
|
116 | 116 | ---
|
117 | 117 |
|
118 |
| -## Only call Hooks from React functions {/*only-call-hooks-from-react-functions*/} |
| 118 | +## 只在 React 函数中调用 Hooks {/*only-call-hooks-from-react-functions*/} |
119 | 119 |
|
120 |
| -Don’t call Hooks from regular JavaScript functions. Instead, you can: |
| 120 | +不要在常规 JavaScript 函数中调用 Hooks 。相反,你可以: |
121 | 121 |
|
122 |
| -✅ Call Hooks from React function components. |
123 |
| -✅ Call Hooks from [custom Hooks](/learn/reusing-logic-with-custom-hooks#extracting-your-own-custom-hook-from-a-component). |
| 122 | +✅ 在 React 函数组件中调用 Hooks 。 |
| 123 | +✅ 在 [自定义 Hooks ](/learn/reusing-logic-with-custom-hooks#extracting-your-own-custom-hook-from-a-component) 中调用 Hooks 。 |
124 | 124 |
|
125 |
| -By following this rule, you ensure that all stateful logic in a component is clearly visible from its source code. |
| 125 | +遵循这个规则,你可以确保组件中的所有有状态逻辑在其源代码中清晰可见。 |
126 | 126 |
|
127 | 127 | ```js {2,5}
|
128 | 128 | function FriendList() {
|
|
0 commit comments