Home › JavaScript

Rendered more hooks than during the previous render. (JavaScript) — how to fix

Uncaught Error: Rendered more hooks than during the previous render.
Uncaught Error: Rendered fewer hooks than expected. This may be caused by an accidental early return statement.

Quick fix: Move every hook (useState, useEffect, useMemo, custom hooks) to the top of the component, before any if (...) return, and never call hooks inside conditions or loops; put the condition inside the hook instead. Enable the react-hooks/rules-of-hooks ESLint rule to catch it.

What it means

React doesn't know hooks by name. It stores state in a list and matches the 1st useState call to slot 1, the 2nd to slot 2, and so on. If one render calls 3 hooks and the next calls 4, React can't match them up and throws:

These are the Rules of Hooks: only call hooks at the top level of a component or custom hook, never conditionally.

Cause 1: a hook after an early return

function Profile({ user }) {
  const [tab, setTab] = useState('posts');
  if (!user) return <Spinner />;           // first render returns here...
  const [likes, setLikes] = useState(0);   // ...so this hook only runs later
  // ...
}

The first render (no user) calls 1 hook, the next (user loaded) calls 2.

Fix 1

Call all hooks first, then return early:

function Profile({ user }) {
  const [tab, setTab] = useState('posts');
  const [likes, setLikes] = useState(0);
  if (!user) return <Spinner />;
  // ...
}

Cause 2: a hook inside a condition or loop

if (isOpen) {
  useEffect(() => { document.title = 'Open'; }, []);
}

items.forEach(item => { const [v] = useState(item); });

Fix 2

Put the condition inside the hook:

useEffect(() => {
  if (isOpen) document.title = 'Open';
}, [isOpen]);

For per-item state, move the item into its own component, so each instance has its own hooks:

{items.map(item => <Row key={item.id} item={item} />)}

function Row({ item }) {
  const [value, setValue] = useState(item.name);
  // ...
}

Cause 3: calling a component as a function

{showDetails && Details()}     // Details uses hooks

Called like this, Details' hooks run as part of the parent, and only when showDetails is true, so the parent's hook count changes.

Fix 3

Render it as an element, so React gives it its own hook list:

{showDetails && <Details />}

Cause 4: a short-circuit around a hook

Expressions like const data = enabled && useQuery(...) or cond ? useA() : useB() call hooks conditionally too.

Fix 4

Always call the hook, and pass the condition as an option or argument. Many data libraries support this directly, e.g. TanStack Query:

const { data } = useQuery({ queryKey: ['user', id], queryFn: fetchUser, enabled: !!id });

Catch it automatically

Install the official ESLint plugin, which flags conditional hook calls as you type:

npm install --save-dev eslint-plugin-react-hooks

Create React App, Next.js and the Vite React templates include it already. Make sure react-hooks/rules-of-hooks is set to error.

Last updated 2026-11-30.