Skip to content
HooksChapter 15 / 37Beginner4 min read

Rules of Hooks

Learn the React rules of hooks, why hooks must be called at the top level of React functions, and how the ESLint plugin helps you follow them.

On this page

1. What are the rules of hooks?

Hooks are functions whose names start with use: useState, useEffect, useRef and so on. They are how you tap into React's features, but they come with a few restrictions.

2. Why does it matter?

React identifies hooks by order, not by name. On every render, hooks must be called in exactly the same order.

Breaking the rulesFollowing the rules
A hook inside an if, which runs sometimes and sometimes notThe same hooks in the same order on every render
React gives state to the wrong hookEach hook gets the right state
Strange bugs and crashesPredictable behavior
Very hard to debugThe linter catches it early

3. Where is it used?

These rules apply everywhere hooks are used:

PlaceAllowed?
Top level of a function componentYes
Top level of a custom hook (name starts with use)Yes
Inside if, for or whileNo
After an early returnNo
Inside an event handler or a useEffect callbackNo
Inside a normal JavaScript functionNo

4. How does it work?

React keeps a list of hooks for each component. The first useState call is slot 1, the second is slot 2, and so on.

The order of hooks
Render 1: useState name, useState age, useEffect
React saves the slots: 1 name, 2 age, 3 effect
Render 2: the same hooks in the same order
Slot 1 goes to name, slot 2 goes to age

If the first hook is skipped in render 2 because of an if, the age hook will read slot 1 and everything shifts by one.

5. Code example

Wrong: a hook inside a condition

jsx
function Profile({ isLoggedIn }) {
  if (isLoggedIn) {
    const [name, setName] = useState("");
  }
  const [theme, setTheme] = useState("light");
}

Right: hooks on top, condition below

jsx
function Profile({ isLoggedIn }) {
  const [name, setName] = useState("");
  const [theme, setTheme] = useState("light");

  if (!isLoggedIn) {
    return <p>Please log in first</p>;
  }

  return <input value={name} onChange={(e) => setName(e.target.value)} />;
}

Linter setup

bash
npm install -D eslint-plugin-react-hooks

How does the code work?

PartMeaning
The first exampleWhen isLoggedIn changes, the number of hooks changes and React gets confused
The second exampleBoth hooks always run, only the JSX is conditional
Early returnIt comes after the hooks, so it is safe
eslint-plugin-react-hooksShows an error in your editor when a rule is broken

The Vite and Next.js templates usually include this plugin already.

6. Common mistakes

Mistake: Writing a hook after an early return

Why? In some renders the function returns early and the hooks below are skipped, which breaks the order.

Better: Write all hooks at the very top of the component, and put returns and conditions after them.

Mistake: Calling useState or useEffect inside an event handler

Why? A handler does not run during render, so there is no slot for the hook and React throws an error.

Better: Create the hook at the top level, and only call the setter inside the handler.

Mistake: Using hooks in a custom function whose name does not start with use

Why? React and the linter treat it as a normal function, so the rules are not checked.

Better: Start the name of any function that uses hooks with use, like useWindowSize.

Interview tip

"Why can't hooks go in loops?" Answer: React tracks hooks by call order. A loop or a condition can change that order, so hooks must stay at the top level.

7. What to learn before and after

Final quick revision

PointDetail
Rule 1Only at the top level, outside conditions and loops
Rule 2Only in components and custom hooks
ReasonReact identifies hooks by order
Helpereslint-plugin-react-hooks
Exceptionuse() can be called conditionally