# Порядок и область вызова React Hooks

https://github.com/sergey-from-riders/vibecoding-base/tree/1d8be88ba9652c0abf9d740de5a9e615d953771c

---

https://github.com/sergey-from-riders/vibecoding-base/blob/1d8be88ba9652c0abf9d740de5a9e615d953771c/registry/standards/frontend/react/rules/hooks.md

---
id: "frontend.hooks"
title: "Порядок и область вызова React Hooks"
section: "frontend"
level: "required"
tags: ["react", "hooks"]
technologies: ["react"]
published_at: "2026-11-13"
reviewed_at: "2026-11-13"
order: 1111
related: ["frontend.derived-state", "frontend.server-client", "frontend.component-naming"]
sources: ["https://react.dev/reference/rules/rules-of-hooks", "https://react.dev/reference/react/use", "https://react.dev/reference/react/useEffect"]
schema_version: 1
---

# Порядок и область вызова React Hooks

## Коротко

useState, useEffect и другие обычные React Hooks вызывай на верхнем уровне функционального компонента или custom Hook, до условных return.

## Плохо

```jsx
import { useState } from 'react';
export function Counter({ enabled }) {
  if (!enabled) return null;
  const [count, setCount] = useState(0);
  return <button onClick={() => setCount(count + 1)}>{count}</button>;
}
```

## Хорошо

```jsx
import { useState } from 'react';
export function Counter({ enabled }) {
  const [count, setCount] = useState(0);
  if (!enabled) return null;
  return <button onClick={() => setCount(value => value + 1)}>{count}</button>;
}
```

## Почему

React связывает обычные Hooks с порядком вызова; скрытый пропуск нарушает связь состояния и render. Cleanup и зависимости обеспечивают корректную синхронизацию с внешней системой.

## Когда применять

Для React Hooks в поддерживаемой версии. Проверены текущие React docs (19.x); исключение API use нельзя переносить на useState/useEffect. В Next.js соответствующие state/effect hooks требуют Client Component; не каждый файл с именем useSomething является серверно совместимым.

### Подробные условия

Не помещай их в цикл, условие, callback, обработчик события или обычный helper. Custom Hook использует имя use + заглавная буква и действительно вызывает Hooks; добавлять use к обычной функции ради стиля не нужно.

Для Effect перечисляй реактивные зависимости и возвращай cleanup подписки/ресурса, когда он нужен. Не подавляй проверку зависимостей для маскировки устаревшего замыкания. API use в React 19+ имеет отдельное исключение: его можно вызывать в условии/цикле, но сохраняются его собственные ограничения, включая React-контекст вызова и запрет try/catch вокруг use.

## Как проверить

- enabled true → false → true → render → порядок Hooks сохраняется и состояние не повреждено.
- Hook вызван из event handler/после условного return → lint/review → нарушение выявлено.
- Effect переподписывается или компонент размонтирован → проверить → предыдущий ресурс освобожден; API use проверен по своим правилам.

## Человек

Определяет нужную внешнюю синхронизацию и lifecycle ресурса.

## Агент

Проверяет порядок и зависимости Hooks, различает специальный use и обычные Hooks, не создает эффект для производного значения.

## Источники

- [React: Rules of Hooks](https://react.dev/reference/rules/rules-of-hooks) — Call order and React function boundaries.
- [React: use](https://react.dev/reference/react/use) — Calling use in loops and conditions; restrictions.
- [React: useEffect](https://react.dev/reference/react/useEffect) — Dependencies, cleanup and race conditions in data fetching.


---

MIT License

Copyright (c) 2026 vibecoding-base contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
