Skip to content

docs(cn): translate reference/rules/rules-of-hooks.md into Chinese #1502

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Apr 29, 2024
62 changes: 31 additions & 31 deletions src/content/reference/rules/rules-of-hooks.md
Original file line number Diff line number Diff line change
@@ -1,61 +1,61 @@
---
title: Rules of Hooks
title: Hook 的规则
---

<Intro>
Hooks are defined using JavaScript functions, but they represent a special type of reusable UI logic with restrictions on where they can be called.
Hook 是使用 JavaScript 函数定义的,但它们代表了一种特殊的可重用的 UI 逻辑,并且对它们可以被调用的位置有限制。
</Intro>

<InlineToc />

---

## Only call Hooks at the top level {/*only-call-hooks-at-the-top-level*/}
## 只在顶层调用 Hook {/*only-call-hooks-at-the-top-level*/}

Functions whose names start with `use` are called [*Hooks*](/reference/react) in React.
在 React 中,以 `use` 开头命名的函数被称为 **[Hook](/reference/react)**。

**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:
**不要在循环、条件语句、嵌套函数或 `try`/`catch`/`finally` 代码块中调用 Hook**。相反,你应该在 React 函数组件的顶层使用 Hook,且在任何提前返回之前。你只能在 React 渲染函数组件时调用 Hook:

* ✅ Call them at the top level in the body of a [function component](/learn/your-first-component).
* ✅ Call them at the top level in the body of a [custom Hook](/learn/reusing-logic-with-custom-hooks).
* ✅ 在 [函数组件主体](/learn/your-first-component) 的顶层调用它们。
* ✅ 在 [自定义 Hook 主体](/learn/reusing-logic-with-custom-hooks) 的顶层调用它们。

```js{2-3,8-9}
function Counter() {
// ✅ Good: top-level in a function component
// ✅ 正确的:在函数组件顶层
const [count, setCount] = useState(0);
// ...
}

function useWindowWidth() {
// ✅ Good: top-level in a custom Hook
// ✅ 正确的:在自定义 Hooks 顶层
const [width, setWidth] = useState(window.innerWidth);
// ...
}
```

It’s **not** supported to call Hooks (functions starting with `use`) in any other cases, for example:
不支持在其他任何情况下调用以 `use` 开头的 Hook,例如:

* 🔴 Do not call Hooks inside conditions or loops.
* 🔴 Do not call Hooks after a conditional `return` statement.
* 🔴 Do not call Hooks in event handlers.
* 🔴 Do not call Hooks in class components.
* 🔴 Do not call Hooks inside functions passed to `useMemo`, `useReducer`, or `useEffect`.
* 🔴 Do not call Hooks inside `try`/`catch`/`finally` blocks.
* 🔴 不要在条件语句或循环中调用 Hook。
* 🔴 不要在条件性的 `return` 语句之后调用 Hook。
* 🔴 不要在事件处理函数中调用 Hook。
* 🔴 不要在类组件中调用 Hook。
* 🔴 不要在传递给 `useMemo``useReducer` `useEffect` 的函数内部调用 Hook。
* 🔴 不要在 `try`/`catch`/`finally` 代码块中调用 Hook。

If you break these rules, you might see this error.
如果你违反了这些规则,你可能会看到以下错误:

```js{3-4,11-12,20-21}
function Bad({ cond }) {
if (cond) {
// 🔴 Bad: inside a condition (to fix, move it outside!)
// 🔴 错误的:在条件语句内部(要修复这个问题,将其移到外部!)
const theme = useContext(ThemeContext);
}
// ...
}

function Bad() {
for (let i = 0; i < 10; i++) {
// 🔴 Bad: inside a loop (to fix, move it outside!)
// 🔴 错误的:在循环语句内部(要修复这个问题,将其移到外部!)
const theme = useContext(ThemeContext);
}
// ...
Expand All @@ -65,22 +65,22 @@ function Bad({ cond }) {
if (cond) {
return;
}
// 🔴 Bad: after a conditional return (to fix, move it before the return!)
// 🔴 错误的:在条件性 return 语句之后(要修复这个问题,将其移到 return 之前!)
const theme = useContext(ThemeContext);
// ...
}

function Bad() {
function handleClick() {
// 🔴 Bad: inside an event handler (to fix, move it outside!)
// 🔴 错误的:在事件处理函数内部(要修复这个问题,将其移到 return 之前!)
const theme = useContext(ThemeContext);
}
// ...
}

function Bad() {
const style = useMemo(() => {
// 🔴 Bad: inside useMemo (to fix, move it outside!)
// 🔴 错误的:在 useMemo 内部调用(要修复这个问题,将其移到外部!)
const theme = useContext(ThemeContext);
return createStyle(theme);
});
Expand All @@ -89,40 +89,40 @@ function Bad() {

class Bad extends React.Component {
render() {
// 🔴 Bad: inside a class component (to fix, write a function component instead of a class!)
// 🔴 错误的:在类组件内部调用(要修复这个问题,改写为函数组件!)
useEffect(() => {})
// ...
}
}

function Bad() {
try {
// 🔴 Bad: inside try/catch/finally block (to fix, move it outside!)
// 🔴 错误的:在 trycatchfinally 代码块内部调用(要修复这个问题,将其移到外部!)
const [x, setX] = useState(0);
} catch {
const [x, setX] = useState(1);
}
}
```

You can use the [`eslint-plugin-react-hooks` plugin](https://www.npmjs.com/package/eslint-plugin-react-hooks) to catch these mistakes.
你可以使用 [`eslint-plugin-react-hooks` 插件](https://www.npmjs.com/package/eslint-plugin-react-hooks) 来捕获这些错误。

<Note>

[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.
[自定义 Hook](/learn/reusing-logic-with-custom-hooks) **可以** 调用其他 Hook(这正是它们的主要目的)。之所以可以这样做,是因为自定义 Hook 也应该只在函数组件渲染时被调用。

</Note>

---

## Only call Hooks from React functions {/*only-call-hooks-from-react-functions*/}
## 仅在 React 函数中调用 Hook {/*only-call-hooks-from-react-functions*/}

Don’t call Hooks from regular JavaScript functions. Instead, you can:
不要在常规的 JavaScript 函数中调用 Hook。相反,你可以:

Call Hooks from React function components.
Call Hooks from [custom Hooks](/learn/reusing-logic-with-custom-hooks#extracting-your-own-custom-hook-from-a-component).
React 函数组件中调用 Hook。
在 [自定义 Hook](/learn/reusing-logic-with-custom-hooks#extracting-your-own-custom-hook-from-a-component) 中调用 Hook。

By following this rule, you ensure that all stateful logic in a component is clearly visible from its source code.
遵循这条规则,你可以确保组件中的所有状态逻辑在其源代码中清晰可见。

```js {2,5}
function FriendList() {
Expand Down
Loading