More React answers

by Danny
Published 2 min read

React throws this when a component calls a different number of hooks from one render to the next. Learn why hook order matters and how to fix conditional hooks and early returns.

The short answer

#

A hook is being called conditionally, usually after an early return or inside an if. On one render React saw fewer hooks, on the next it saw more, and it cannot match them up. Move every hook call to the top of the component, above any early return, and put the condition inside the hook instead.


_10
function Profile({ user }: { user: User | null }) {
_10
- if (!user) return <SignInPrompt />;
_10
- const [tab, setTab] = useState('posts');
_10
+ const [tab, setTab] = useState('posts');
_10
+ if (!user) return <SignInPrompt />;
_10
_10
return <Tabs value={tab} onChange={setTab} />;
_10
}

Why it happens

#

React does not store hook state by name. It keeps a list for each component and hands out the entries in order: the first useState call gets slot one, the next hook gets slot two, and so on. This only works if the component calls the same hooks in the same order on every render.

When a hook sits behind a condition, the list changes length. If the condition was false last time and true this time, React finds an extra hook it has no record of and throws "Rendered more hooks than during the previous render". The reverse case, where a hook is skipped, produces the related error "Rendered fewer hooks than expected. This may be caused by an accidental early return statement."

The usual causes are:

  • An early return for a loading, error or empty state placed above a hook.
  • A hook inside an if, a switch or a && expression.
  • A hook inside a loop whose length changes between renders.
  • Calling a component as a plain function, such as Row(props) in a .map, so its hooks become part of the parent's list.

How to fix it

#

Call hooks first, return early afterwards

The most common fix is simply reordering. Put every hook at the top level of the component, then do the early returns.


_10
function Dashboard({ userId }: { userId: string | null }) {
_10
const [range, setRange] = useState('7d');
_10
const stats = useStats(userId, range);
_10
_10
if (!userId) return <SignInPrompt />;
_10
if (!stats) return <Spinner />;
_10
_10
return <StatsView stats={stats} range={range} onRangeChange={setRange} />;
_10
}

Move the condition inside the hook

If a hook should only do work in some cases, call it every time and let it decide. Most data and effect hooks accept a way to switch themselves off.


_10
useEffect(() => {
_10
if (!isOpen) return;
_10
const id = setInterval(refresh, 5000);
_10
return () => clearInterval(id);
_10
}, [isOpen]);

For a custom hook, accept a nullable argument or an enabled flag rather than wrapping the call in an if.

Split the conditional part into its own component

When a whole block of hooks only applies in one branch, move that branch into a child component. Each component has its own hook list, so rendering or not rendering the child is fine.


_10
function Profile({ user }: { user: User | null }) {
_10
if (!user) return <SignInPrompt />;
_10
return <SignedInProfile user={user} />;
_10
}
_10
_10
function SignedInProfile({ user }: { user: User }) {
_10
const [tab, setTab] = useState('posts');
_10
const posts = usePosts(user.id);
_10
return <ProfileView tab={tab} onTabChange={setTab} posts={posts} />;
_10
}

Render components with JSX, not by calling them

If you call a component like a function, its hooks run inside the parent. Render it with JSX so React treats it as its own component.


_10
- {rows.map((row) => Row({ row }))}
_10
+ {rows.map((row) => <Row key={row.id} row={row} />)}

Common pitfalls

#
  • Ignoring the lint rule. The react-hooks/rules-of-hooks rule in eslint-plugin-react-hooks catches nearly every case of this before it reaches the browser. Turn it on as an error.
  • Thinking use is the exception for everything. React 19's use API can be called inside conditions, but that does not apply to useState, useEffect or any other hook.
  • Hiding hooks in helpers. A plain function that calls a hook is a hook too. Name it useSomething and follow the same rules so the linter can check it.
  • Fixing it with a key. Adding a key to force a remount can hide the error but resets all state. Reorder the hooks instead.
#

Was this resource helpful?