Home / Learn React / What Are Hooks? Rules of Hooks
What Are Hooks? Rules of Hooks
Hooks are special functions that start with use. They let a function component keep state, run side effects, read context and more.
Theory in plain words
What it is
A hook is a function that lets your component "hook into" React features. useState remembers values, useEffect runs code after render, useContext reads shared data.
Why we use it
Before hooks only class components could have state and lifecycle methods. Hooks give function components the same power with less code, and let you share logic between components with custom hooks.
How it works
React remembers hook state by the order in which hooks are called during a render. That is why the order must be the same every time: no hooks inside conditions or loops.
Where it is used
- Every modern React app
- Sharing logic: useFetch, useLocalStorage, useDebounce
- Libraries: React Router, TanStack Query and Redux all offer hooks
The two rules
- Only call hooks at the top level. Not inside
if, loops, nested functions or after an earlyreturn. - Only call hooks from React functions: function components or your own custom hooks (names starting with
use).
React notices that the number of hooks changed between renders and throws an error.
Why the order matters
React does not know hook names. It stores your hooks in a list and matches each call to a slot by its position. If a condition skips a call, every following hook gets the wrong slot.
eslint-plugin-react-hooks checks both rules for you. Vite templates include it.Quick quiz
1. Where may you call a hook?
Hooks must be called in the same order every render.
2. Why does the order of hooks matter?
Hook state is kept in a list by position.
3. A custom hook name must start with...
The use prefix lets React and the linter treat it as a hook.
Common questions
Where may you call a hook?
At the top level of a component or custom hook. Hooks must be called in the same order every render.
Why does the order of hooks matter?
React matches hooks to stored state by call order. Hook state is kept in a list by position.