Error: Invalid hook call. Hooks can only be called inside of the body of a function component.
A hook ran outside a function component/custom hook, conditionally — or two copies of React are loaded.
Seen on:
React
Meaning
React lists three causes and they are exactly the ones to check: mismatched React and renderer versions, breaking the Rules of Hooks, or more than one copy of React in the bundle (common with npm link / monorepos / libraries bundling React).
Common causes
- Calling a hook in a class component, a regular function, or an event handler
- Calling hooks conditionally or inside loops
- Two copies of React (library lists react as dependency instead of peerDependency, npm link)
- react and react-dom versions differ
⚡ Quick fix
- Only call hooks at the top level of function components or custom hooks (
useX) - Check duplicates:
npm ls react - Make libraries use react as a peerDependency
- Align react and react-dom versions
Detailed fix by platform
React
- Find duplicate React copies:bash
npm ls react react-dom # monorepo/linked lib: point the bundler at one copy, e.g. Vite # resolve: { dedupe: ['react', 'react-dom'] }
Code examples
Hook inside a callback
jsx
function Profile() {
const onClick = () => {
const [open, setOpen] = useState(false); // ❌ hook inside a handler
};
const [open, setOpen] = useState(false); // ✅ top level of the component
}How to diagnose
- Location — Where is the hook called?
- Duplicates —
npm ls react— more than one? - Versions — react vs react-dom versions
🧠 Still stuck? Analyze your error
Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.
Was this page helpful?
Report a correction or suggest an improvement
Last updated 2 Oct 2026