React says a component rendered fewer hooks than the last render
Uncaught 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.
Fix it fast
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.jsx2Match what you see, then jump to its fix
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.jsx2Fix
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.jsxConfirmed by react.dev
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.jsx2Fix
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 srcConfirmed by react.dev
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- TaskvareA task and project-management suite: React on the web, Flutter on mobile, TypeScript underneath.
Related fixes
Web apps
Module not found for antd/dist/antd.less after upgrading to Ant Design 5
Module not found: Error: Can't resolve 'antd/dist/antd.less'
Ant Design 5 dropped Less and the bundled CSS file for CSS-in-JS, so antd/dist/antd.less no longer exists.
3 causes, each with a fixWeb apps
Astro Code component fails inside a React component
Astro components cannot be rendered directly via function call
Astro components such as Code only exist on the server and render static HTML.
2 causes, each with a fixWeb apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
How to catch and handle a 422 from Axios in a Redux thunk
action.error.message is 'Rejected' and action.payload is undefined
A 422 means the server read the request but cannot process its content, so repeating it unchanged fails again.
4 causes, each with a fixWeb apps
axios gets 429 Too Many Requests from a React app
429 (Too Many Requests)
A 429 comes from the server or an API gateway, not from axios: your client sent too many requests in a window.
4 causes, each with a fixWeb apps
Axios request is not cancelled when a React component unmounts
A cancelled request is logged as an error in the catch block
Axios cancels a request when you pass it an AbortSignal and later call abort() on its controller.
4 causes, each with a fix