Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

React says a component rendered fewer hooks than the last render

ErrorUncaught Error: Rendered fewer hooks than expected. This may be caused by an accidental early return statement.

Short answer

React matches hooks to state by call order, so every render of a component must call the same hooks in the same order. The error means one render called fewer. Find the hook that sits after a return, inside an if or loop, or inside try/catch, and move it to the top of the component.

  • Applies to React 19, eslint-plugin-react-hooks
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Open the component named in the stack trace and search it for the first return. Every use... call (useState, useEffect, useContext, your own hooks) must be above that line. Then let the lint rule list the rest.

    npx eslint --rule '{"react-hooks/rules-of-hooks": "error"}' src/components/Profile.jsx
  2. 2Match what you see, then jump to its fix

    1. 1The component has `if (...) return null;` and a useState, useEffect or useContext below itA hook is called after an early returnFix
    2. 2A use... call is indented under if, for, map-style loops or try in the same componentA hook sits inside an if, a loop or a try/catchFix

What does the error look like?

  • Uncaught Error: Rendered fewer hooks than expected. This may be caused by an accidental early return statement.

Cause 1 of 2

A hook is called after an early return

1Check

On the first render the condition is true and React stops before the later hooks. On a later render it is false and more hooks run, or the other way round, so the count differs.

grep -n -E 'return|use[A-Z][A-Za-z]*\(' src/components/Profile.jsx

2Fix

Call all hooks first. Put the early return after the last hook, and put the condition inside the hook if it should only act sometimes.

function Profile({ user }) {
  const [open, setOpen] = useState(false);   // hooks first
  useEffect(() => {
    if (!user) return;                         // condition inside the hook
    track(user.id);
  }, [user]);

  if (!user) return null;                      // early return last
  return <Card open={open} user={user} />;
}

3Verify

Load the screen in both states (user missing, user present) and switch between them. The error does not return.

npx eslint src/components/Profile.jsx

Confirmed by react.dev

Did this fix it?

Cause 2 of 2

A hook sits inside an if, a loop or a try/catch

1Check

Hooks may not be called inside conditions, loops, nested functions or try/catch/finally blocks. A branch that runs on some renders only changes the number of hooks.

grep -n -B2 'use[A-Z][A-Za-z]*(' src/components/Profile.jsx

2Fix

Move the call outside the branch. If you need one hook per list item, move the hook into a child component that you render once per item.

// before: hook inside a loop
// items.forEach(i => { const [v, setV] = useState(0); });

function Row({ item }) {
  const [v, setV] = useState(0);   // one call per Row component
  return <li onClick={() => setV(v + 1)}>{item.name}: {v}</li>;
}
// in the parent: items.map(i => <Row key={i.id} item={i} />)

3Verify

Add and remove an item in the list. The component keeps working with any number of items.

npx eslint src

Confirmed by react.dev

Did this fix it?

How do I stop it coming back?

  • Enable eslint-plugin-react-hooks and treat rules-of-hooks as an error, not a warning.
  • Write hooks first in every component, then the early returns, then the JSX.
  • Use a condition inside useEffect or useMemo instead of around it.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.