React · Frontend system thinking

What are the rules of hooks and why do they exist?

Mediumhooksrules of hookscall orderUpdated Oct 2026
Quiz this track

Short answer

Call hooks only at the top level of React functions, not conditionally, in loops or after early returns. React matches hooks by call order, so the order must be identical on every render.

React stores hook state in a linked list attached to the component's Fiber. On each render it walks that list in the same order the hooks are called. There is no hook name or ID to look up, so the only thing connecting a useState call to its stored value is its position in the sequence.

jsx
function Profile({ user }) {
  const [open, setOpen] = useState(false);

  if (!user) {
    return <p>No user</p>;
  }

  useEffect(() => {
    document.title = user.name;
  }, [user.name]);

  return <h1>{user.name}</h1>;
}
This is valid because every hook still runs in the same order. An early return after all hooks is fine.

What breaks the rules

  • Calling a hook inside an if, loop or callback
  • Returning before a hook that runs on other renders
  • Calling a hook from a regular JavaScript helper
  • Calling a hook from a class component
jsx
function Bad({ enabled }) {
  if (enabled) {
    useEffect(() => {
      console.log("enabled");
    }, []);
  }

  const [count, setCount] = useState(0);
  return <button onClick={() => setCount(count + 1)}>{count}</button>;
}
When enabled flips, the effect is skipped and useState reads the wrong slot. Never do this.

The rule is about call order, not about where the code lives. A custom hook can contain conditionals as long as its own hook calls remain unconditional. If you need conditional behaviour, put the condition inside the hook or split the component into separate children.

The eslint-plugin-react-hooks rules catch most violations. Do not silence them with disable comments unless you have a very specific reason and understand the render-order consequence.

Follow-ups you should expect

Interviewers use these to check whether the first answer was understanding or memorisation.

  1. 1Why can't hooks be called conditionally?
  2. 2Can a custom hook call another hook conditionally?
  3. 3Why does an early return after all hooks work?
  4. 4What does the exhaustive-deps lint rule enforce?

Say it in the interview

React matches hook state by call order, not by name. That means every render of a component must call the same hooks in the same sequence. I keep hooks at the top level, before any early return, and never put them in conditions, loops or nested functions. If behaviour is conditional, the condition goes inside the effect or the component is split, rather than changing the hook order.

Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.

Related questions

Reading is step one

Now say it out loud.

Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live React mock interview and get scored feedback on depth, clarity and confidence.