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
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
+
Хуки определяются как функции JavaScript, но они представляют собой особый тип повторно используемой логики пользовательского интерфейса с ограничениями на то, где они могут быть вызваны.
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
+
## Только вызывайте хуки на верхнем уровне {/*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`, называются [*хуками*](/reference/react)в 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`.**Вместо этого всегда используйте хуки на верхнем уровне вашей функции React, перед любыми ранними возвратами. Вы можете вызывать хуки только во время рендеринга React для функционального компонента:
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
+
* ✅ Вызывайте их на верхнем уровне в теле [пользовательского хука](/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
+
// ✅ Хорошо: на верхнем уровне в функциональном компоненте
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
+
// ✅ Хорошо: на верхнем уровне в пользовательском хуке
// 🔴 Плохо: внутри блока try/catch/finally (чтобы исправить, вынесите его наружу!)
101
101
const [x, setX] = useState(0);
102
102
} catch {
103
103
const [x, setX] = useState(1);
104
104
}
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
+
[Пользовательские хуки](/learn/reusing-logic-with-custom-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 {/*only-call-hooks-from-react-functions*/}
119
119
120
-
Don’t call Hooks from regular JavaScript functions. Instead, you can:
120
+
Не вызывайте хуки из обычных функций JavaScript. Вместо этого вы можете:
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.
123
+
✅ Вызывать хуки из [пользовательских хуков](/learn/reusing-logic-with-custom-hooks#extracting-your-own-custom-hook-from-a-component).
124
124
125
-
By following this rule, you ensure that all stateful logic in a component is clearly visible from its source code.
125
+
Следуя этому правилу, вы гарантируете, что вся логика состояния в компоненте будет четко видна из его исходного кода.
0 commit comments