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
Copy file name to clipboardexpand all lines: content/docs/hooks-rules.md
+24-24
Original file line number
Diff line number
Diff line change
@@ -1,31 +1,31 @@
1
1
---
2
2
id: hooks-rules
3
-
title: Rules of Hooks
3
+
title: 使用 Hook 的规则
4
4
permalink: docs/hooks-rules.html
5
5
next: hooks-custom.html
6
6
prev: hooks-effect.html
7
7
---
8
8
9
-
*Hooks* are a new addition in React 16.8. They let you use state and other React features without writing a class.
9
+
*Hook* 是 React 16.8 的新增特性。它可以让你在不使用 class 的情况下使用 state 和一些其他 React 特性。
10
10
11
-
Hooks are JavaScript functions, but you need to follow two rules when using them. We provide a [linter plugin](https://www.npmjs.com/package/eslint-plugin-react-hooks) to enforce these rules automatically:
**Don't call Hooks inside loops, conditions, or nested functions.** Instead, always use Hooks at the top level of your React function. By following this rule, you ensure that Hooks are called in the same order each time a component renders. That's what allows React to correctly preserve the state of Hooks between multiple `useState`and`useEffect`calls. (If you're curious, we'll explain this in depth [below](#explanation).)
By following this rule, you ensure that all stateful logic in a component is clearly visible from its source code.
24
+
遵循此规则,确保组件的状态逻辑在代码中清晰可见。
25
25
26
-
## ESLint Plugin {#eslint-plugin}
26
+
## ESLint 插件 {#eslint-plugin}
27
27
28
-
We released an ESLint plugin called [`eslint-plugin-react-hooks`](https://www.npmjs.com/package/eslint-plugin-react-hooks)that enforces these two rules. You can add this plugin to your project if you'd like to try it:
In the future, we intend to include this plugin by default into Create React App and similar toolkits.
49
+
我们打算后续版本默认添加此插件到 Create React App 及其他类似的工具包中。
50
50
51
-
**You can skip to the next page explaining how to write [your own Hooks](/docs/hooks-custom.html) now.** On this page, we'll continue by explaining the reasoning behind these rules.
As we [learned earlier](/docs/hooks-state.html#tip-using-multiple-state-variables), we can use multiple State or Effect Hooks in a single component:
55
+
正如我们[之前学到](/docs/hooks-state.html#tip-using-multiple-state-variables)的,我们可以在单个组件中使用多个 State Hook 或 Effect Hook
56
56
57
57
```js
58
58
functionForm() {
@@ -76,7 +76,7 @@ function Form() {
76
76
}
77
77
```
78
78
79
-
So how does React know which state corresponds to which `useState` call? The answer is that **React relies on the order in which Hooks are called**. Our example works because the order of the Hook calls is the same on every render:
@@ -98,7 +98,7 @@ useEffect(updateTitle) // 4. Replace the effect for updating the title
98
98
// ...
99
99
```
100
100
101
-
As long as the order of the Hook calls is the same between renders, React can associate some local state with each of them. But what happens if we put a Hook call (for example, the `persistForm` effect) inside a condition?
// 🔴 We're breaking the first rule by using a Hook in a condition
@@ -109,7 +109,7 @@ As long as the order of the Hook calls is the same between renders, React can as
109
109
}
110
110
```
111
111
112
-
The`name !== ''`condition is `true` on the first render, so we run this Hook. However, on the next render the user might clear the form, making the condition `false`. Now that we skip this Hook during rendering, the order of the Hook calls becomes different:
useState('Mary') // 1. Read the name state variable (argument is ignored)
@@ -118,9 +118,9 @@ useState('Poppins') // 🔴 2 (but was 3). Fail to read the surname state
118
118
useEffect(updateTitle) // 🔴 3 (but was 4). Fail to replace the effect
119
119
```
120
120
121
-
React wouldn't know what to return for the second `useState` Hook call. React expected that the second Hook call in this component corresponds to the `persistForm` effect, just like during the previous render, but it doesn't anymore. From that point, every next Hook call after the one we skipped would also shift by one, leading to bugs.
**This is why Hooks must be called on the top level of our components.** If we want to run an effect conditionally, we can put that condition *inside* our Hook:
@@ -131,8 +131,8 @@ React wouldn't know what to return for the second `useState` Hook call. React ex
131
131
});
132
132
```
133
133
134
-
**Note that you don't need to worry about this problem if you use the [provided lint rule](https://www.npmjs.com/package/eslint-plugin-react-hooks).** But now you also know *why* Hooks work this way, and which issues the rule is preventing.
Finally, we're ready to learn about [writing your own Hooks](/docs/hooks-custom.html)! Custom Hooks let you combine Hooks provided by React into your own abstractions, and reuse common stateful logic between different components.
0 commit comments