Skip to content

Commit 60b5ed9

Browse files
JohnhanLiuYucohny
andauthored
docs(cn): translate warnings/invalid-hook-call-warning to Chinese (#1153)
Co-authored-by: Yucohny <yucohny@163.com>
1 parent 4ec3d78 commit 60b5ed9

File tree

1 file changed

+44
-44
lines changed

1 file changed

+44
-44
lines changed
Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,68 @@
11
---
2-
title: Rules of Hooks
2+
title: Hook 规则
33
---
44

5-
You are probably here because you got the following error message:
5+
你进入到这个页面,大概是因为遇到了下面这个错误提示:
66

77
<ConsoleBlock level="error">
88

99
Hooks can only be called inside the body of a function component.
1010

1111
</ConsoleBlock>
1212

13-
There are three common reasons you might be seeing it:
13+
通常有以下 3 个错误原因:
1414

15-
1. You might be **breaking the Rules of Hooks**.
16-
2. You might have **mismatching versions** of React and React DOM.
17-
3. You might have **more than one copy of React** in the same app.
15+
1. 你可能 **打破了 Hook 的使用规则**
16+
2. 你可能使用了 **版本不一致** React React DOM
17+
3. 你可能在同一个应用当中使用了 **重复的 React 引用**
1818

19-
Let's look at each of these cases.
19+
让我们来逐个看看这些问题。
2020

21-
## Breaking Rules of Hooks {/*breaking-rules-of-hooks*/}
21+
## 打破了 Hook 的使用规则 {/*breaking-rules-of-hooks*/}
2222

23-
Functions whose names start with `use` are called [*Hooks*](/reference/react) in React.
23+
在 React 中被调用的且以 `use` 开头命名的函数叫 [**Hook**](/reference/react)
2424

25-
**Don’t call Hooks inside loops, conditions, or nested functions.** 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:
25+
**不要在循环语句内、条件语句后或嵌套的函数内调用 Hook**。反之,应该始终保证 Hook 在函数式组件的顶层,并避免在 Hook 调用前过早终止函数。你只能在 React 渲染函数式组件的过程当中调用 Hook:
2626

27-
*Call them at the top level in the body of a [function component](/learn/your-first-component).
28-
*Call them at the top level in the body of a [custom Hook](/learn/reusing-logic-with-custom-hooks).
27+
*[函数式组件](/learn/your-first-component) 内部的顶级作用域调用他们。
28+
*[自定义 Hook](/learn/reusing-logic-with-custom-hooks) 内部的顶级作用域调用他们。
2929

3030
```js{2-3,8-9}
3131
function Counter() {
32-
// ✅ Good: top-level in a function component
32+
// ✅ 正确:函数式组件的顶级作用域
3333
const [count, setCount] = useState(0);
3434
// ...
3535
}
3636
3737
function useWindowWidth() {
38-
// ✅ Good: top-level in a custom Hook
38+
// ✅ 正确:自定义 Hook 的顶级作用域
3939
const [width, setWidth] = useState(window.innerWidth);
4040
// ...
4141
}
4242
```
4343

44-
It’s **not** supported to call Hooks (functions starting with `use`) in any other cases, for example:
44+
以下有几个要点,这些情况下 **** 支持调用 Hook(以 `use` 开头的函数),例如:
4545

46-
* 🔴 Do not call Hooks inside conditions or loops.
47-
* 🔴 Do not call Hooks after a conditional `return` statement.
48-
* 🔴 Do not call Hooks in event handlers.
49-
* 🔴 Do not call Hooks in class components.
50-
* 🔴 Do not call Hooks inside functions passed to `useMemo`, `useReducer`, or `useEffect`.
46+
* 🔴 不要在条件语句内或循环语句内调用 Hook。
47+
* 🔴 不要在包含 `return` 的条件语句之后调用 Hook。
48+
* 🔴 不要在事件监听中调用 Hook。
49+
* 🔴 不要在类式组件内调用 Hook。
50+
* 🔴 不要在那些传给 `useMemo``useReducer` `useEffect` 的函数内调用 Hook。
5151

52-
If you break these rules, you might see this error.
52+
如果你打破了这些规则,你可能就会看到这个错误提示。
5353

5454
```js{3-4,11-12,20-21}
5555
function Bad({ cond }) {
5656
if (cond) {
57-
// 🔴 Bad: inside a condition (to fix, move it outside!)
57+
// 🔴 错误:在条件语句内调用(修复办法:把它挪到外层!)
5858
const theme = useContext(ThemeContext);
5959
}
6060
// ...
6161
}
6262
6363
function Bad() {
6464
for (let i = 0; i < 10; i++) {
65-
// 🔴 Bad: inside a loop (to fix, move it outside!)
65+
// 🔴 错误:在循环内调用(修复办法:把它挪到外层!)
6666
const theme = useContext(ThemeContext);
6767
}
6868
// ...
@@ -72,22 +72,22 @@ function Bad({ cond }) {
7272
if (cond) {
7373
return;
7474
}
75-
// 🔴 Bad: after a conditional return (to fix, move it before the return!)
75+
// 🔴 错误:在包含 return 的条件语句后调用(修复办法:挪到 return 之前!)
7676
const theme = useContext(ThemeContext);
7777
// ...
7878
}
7979
8080
function Bad() {
8181
function handleClick() {
82-
// 🔴 Bad: inside an event handler (to fix, move it outside!)
82+
// 🔴 错误:在事件监听器或回调中调用(修复办法:把它挪到外层!)
8383
const theme = useContext(ThemeContext);
8484
}
8585
// ...
8686
}
8787
8888
function Bad() {
8989
const style = useMemo(() => {
90-
// 🔴 Bad: inside useMemo (to fix, move it outside!)
90+
// 🔴 错误:在 useMemo 内调用(修复办法:把它挪到外层!)
9191
const theme = useContext(ThemeContext);
9292
return createStyle(theme);
9393
});
@@ -96,63 +96,63 @@ function Bad() {
9696
9797
class Bad extends React.Component {
9898
render() {
99-
// 🔴 Bad: inside a class component (to fix, write a function component instead of a class!)
99+
// 🔴 错误:在类式组件内调用(修复办法:写一个函数式组件而不是类式组件!)
100100
useEffect(() => {})
101101
// ...
102102
}
103103
}
104104
```
105105

106-
You can use the [`eslint-plugin-react-hooks` plugin](https://www.npmjs.com/package/eslint-plugin-react-hooks) to catch these mistakes.
106+
你可以借助 [`eslint-plugin-react-hooks` 插件](https://www.npmjs.com/package/eslint-plugin-react-hooks) 来帮你提前暴露这些错误。
107107

108108
<Note>
109109

110-
[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.
110+
[自定义 Hook](/learn/reusing-logic-with-custom-hooks) **可能** 被其他的 Hook 调用 (这仍然符合设计初衷)。为什么呢?因为自定义 Hook 也是被设定为只能在函数式组件渲染过程中被调用。
111111

112112
</Note>
113113

114-
## Mismatching Versions of React and React DOM {/*mismatching-versions-of-react-and-react-dom*/}
114+
## 版本不一致的 React React DOM {/*mismatching-versions-of-react-and-react-dom*/}
115115

116-
You might be using a version of `react-dom` (< 16.8.0) or `react-native` (< 0.59) that doesn't yet support Hooks. You can run `npm ls react-dom` or `npm ls react-native` in your application folder to check which version you're using. If you find more than one of them, this might also create problems (more on that below).
116+
你可能正在使用一个还不支持 Hook 的版本,例如 `react-dom`< 16.8.0)或 `react-native`< 0.59)。你可以在你的应用目录下执行 `npm ls react-dom` `npm ls react-native` 来检查下你正在使用的版本。如果你发现了同一个包有多个版本,那也可能带来其他的问题(下文会详细展开)。
117117

118-
## Duplicate React {/*duplicate-react*/}
118+
## 重复的 React {/*duplicate-react*/}
119119

120-
In order for Hooks to work, the `react` import from your application code needs to resolve to the same module as the `react` import from inside the `react-dom` package.
120+
为了使 Hook 正常工作,你需要确保你应用代码中所引用的 `react` `react-dom` 内部使用的 `react` 是同一个来源。
121121

122-
If these `react` imports resolve to two different exports objects, you will see this warning. This may happen if you **accidentally end up with two copies** of the `react` package.
122+
如果上述的两个 `react` 是使用不同模块导出的值,你可能会看到这个警告信息。一般来说,原因都会是你 **意外地使用了重复的** `react` 包。
123123

124-
If you use Node for package management, you can run this check in your project folder:
124+
如果你用的是 Node 进行包管理,你可以在你的应用目录下执行这个命令做个检查:
125125

126126
<TerminalBlock>
127127

128128
npm ls react
129129

130130
</TerminalBlock>
131131

132-
If you see more than one React, you'll need to figure out why this happens and fix your dependency tree. For example, maybe a library you're using incorrectly specifies `react` as a dependency (rather than a peer dependency). Until that library is fixed, [Yarn resolutions](https://yarnpkg.com/lang/en/docs/selective-version-resolutions/) is one possible workaround.
132+
如果你看到了超过 1 个 `React`,你可能需要去搞明白为什么会这样,并且修复一下你的包依赖关系。举个例子:你可能用了一个包,其内部错误地声明了 `react` 作为它的依赖(推荐做法应该是在 peerDependency 中)。在这个包被修复之前,[yarn resolutions](https://yarnpkg.com/lang/en/docs/selective-version-resolutions/) 可以作为一个临时解决办法。
133133

134-
You can also try to debug this problem by adding some logs and restarting your development server:
134+
你也可以通过增加一些日志,然后重启你的开发服务器,这样你就可以自己来调试这个问题了:
135135

136136
```js
137-
// Add this in node_modules/react-dom/index.js
137+
// 把下面这行加在 node_modules/react-dom/index.js
138138
window.React1 = require('react');
139139

140-
// Add this in your component file
140+
// 把下面这几行加入到你的组件逻辑中
141141
require('react-dom');
142142
window.React2 = require('react');
143143
console.log(window.React1 === window.React2);
144144
```
145145

146-
If it prints `false` then you might have two Reacts and need to figure out why that happened. [This issue](https://github.com/facebook/react/issues/13991) includes some common reasons encountered by the community.
146+
如果你在控制台看到打印了 `false`,那代表你的项目中存在两个 React,你需要搞明白这是为什么。[ issue](https://github.com/facebook/react/issues/13991) 列举了一些常见的可能的原因。
147147

148-
This problem can also come up when you use `npm link` or an equivalent. In that case, your bundler might "see" two Reacts — one in application folder and one in your library folder. Assuming `myapp` and `mylib` are sibling folders, one possible fix is to run `npm link ../myapp/node_modules/react` from `mylib`. This should make the library use the application's React copy.
148+
如果你使用了 `npm link` 或者同类操作,也有可能导致这个问题出现。在这种情况下,你的构建工具可能会“看到”两个不同的 React——一个在应用目录,另一个则在工具库的目录。假设 `myapp` `mylib` 是两个相邻的目录,一个可能有效的解决办法是在 `mylib` 目录下执行 `npm link ../myapp/node_modules/react`,这样就能让工具库里面使用的 React 和你应用里面使用的是同一个了。
149149

150150
<Note>
151151

152-
In general, React supports using multiple independent copies on one page (for example, if an app and a third-party widget both use it). It only breaks if `require('react')` resolves differently between the component and the `react-dom` copy it was rendered with.
152+
通常来讲,在一个页面上使用多个相互独立的 React 是完全没问题的(举个例子,应用和第三方库同时使用各自的 React)。只有当你组件里引用的 `react``react-dom` 里引用的 `react` 不一致时,才会导致这个报错。
153153

154154
</Note>
155155

156-
## Other Causes {/*other-causes*/}
156+
## 其他原因 {/*other-causes*/}
157157

158-
If none of this worked, please comment in [this issue](https://github.com/facebook/react/issues/13991) and we'll try to help. Try to create a small reproducing example — you might discover the problem as you're doing it.
158+
如果上文没有解决你的问题,你可以在 [ issue](https://github.com/facebook/react/issues/13991) 中提交评论,我们会积极地提供帮助。评论的时候,如果能提供一个小的、能复现的示例那最好不过了。

0 commit comments

Comments
 (0)