Fix guides · Lovable, Bolt, Cursor, Claude Code, v0 ·
Fix React "Invalid hook call": causes and fixes
Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons: 1. You might have mismatching versions of React and the renderer (such as React DOM) 2. You might be breaking the Rules of Hooks 3. You might have more than one copy of React in the same app
"Invalid hook call" means React saw a hook (useState, useEffect, useContext or a custom useSomething) run outside a component while it was rendering. Either the hook is called in the wrong place (an event handler, a normal function, inside useEffect), or your app has two copies of React or mismatched react and react-dom versions, so the hook talks to the wrong React.
Common causes – and how to tell which one you have
1. A hook is called inside an event handler or helper
How to tell: useState, useNavigate, useContext or a custom useX is called inside onClick, onSubmit, a fetch helper or a plain function that isn't a component or hook.
Fix: Move the hook call to the top level of the component and use its value inside the handler. For example, call const navigate = useNavigate() at the top and use navigate('/home') in onClick.
2. A hook is called inside useEffect, useMemo or a callback
How to tell: The code looks like useEffect(() => { const user = useUser(); ... }). Hooks inside other hooks' callbacks are not allowed.
Fix: Call the hook at the top level of the component, then use its value inside the effect and add it to the dependency array.
3. react and react-dom versions don't match
How to tell: npm ls react react-dom shows different major versions (e.g. react 19 and react-dom 18), often after an AI upgraded only one of them.
Fix: Install matching versions together, e.g. npm install react@19 react-dom@19 (and matching @types/react, @types/react-dom), then restart the dev server.
4. Two copies of React in the app
How to tell: npm ls react shows React twice, for example inside a UI library's own node_modules, a linked local package, or a monorepo workspace.
Fix: Make the library use your React: reinstall so it's deduped, make sure the library lists react as a peerDependency, or add resolve: { dedupe: ['react', 'react-dom'] } in vite.config.
5. A component is called as a function from outside React
How to tell: A component that uses hooks is called like Modal() from an event handler or utility code, instead of being rendered as <Modal />.
Fix: Render it as JSX (<Modal />) and control it with state, for example const [open, setOpen] = useState(false).
How to fix it
- Open the component named in the stack trace and find the hook call React complains about.
- Check that the hook is called at the top level of a function component or a custom hook, not inside an event handler, loop, condition, useEffect or a normal helper function.
- Check that the component is rendered as <MyComponent />, not called like a normal function from outside React.
- Run npm ls react react-dom and make sure there is exactly one version of each and the major versions match.
- If you see two copies of React (often from a UI library or a monorepo/linked package), dedupe them: reinstall, fix the library's peer dependency, or set resolve.dedupe in Vite.
- Restart the dev server and reload the page to confirm the error is gone.
Using Lovable, Bolt or v0?
In Lovable or Bolt, paste the full error with the stack trace and say: "Find the hook that's called outside a component's top level and move it to the top of the component. Also check that react and react-dom have the same version."
Using Cursor, Claude Code or Windsurf?
Ask Cursor or Claude Code to find the hook from the stack trace, check where it's called, and run npm ls react react-dom. Have it turn on the eslint-plugin-react-hooks rules so the same mistake is flagged in the editor next time.
The “fix” to avoid
Agents sometimes turn the component into a class, wrap the hook in try/catch or add conditions around it. That doesn't fix the cause and can trigger "Rendered more hooks than during the previous render". Move the hook to the top level or fix the duplicate React instead.
Copy-paste fix prompt for your AI coding tool
Paste this into Cursor, Claude Code, Lovable or Bolt:
My React app throws "Invalid hook call. Hooks can only be called inside of the body of a function component". Find the hook from the stack trace. Check: 1) is it called inside an event handler, loop, condition, useEffect callback or a normal (non-hook) function? Move it to the top level of the component or a custom hook and use its result there; 2) does npm ls react react-dom show more than one copy or mismatched major versions? Align them and dedupe (resolve.dedupe in Vite for react and react-dom). Don't wrap hooks in try/catch. Show me the cause you found and the change.
Questions people ask
Why did this start after installing a UI library?
The library probably brought its own copy of React. Run npm ls react. If you see two versions, align them or make the library use your React (peer dependency or dedupe).
Can I use hooks in a Next.js Server Component?
No. Hooks like useState and useEffect need 'use client' at the top of the file. Next.js then shows a different, clearer error than this one.
Is it okay to call a hook inside an if?
No. Hooks must run in the same order on every render. Call the hook unconditionally at the top and put the if inside or after it.