Error library / this error
How to fix it
Invalid hook call. Hooks can only be called inside of the body of a function component.
What it means
A React hook ran somewhere React does not allow it, or there are two copies of React in your app.
Common causes
- The hook is called inside a loop, condition, event handler or regular function.
- The component is a class component.
- Your app and a library each bundle their own copy of React (common with npm link or monorepos).
- react and react-dom versions do not match.
How to fix it
1. Call hooks at the top level
function Profile({ id }) {
const user = useUser(id); // always runs, in the same order
if (!user) return null;
return <h1>{user.name}</h1>;
}
2. Check for duplicate React copies
Run npm ls react. You should see exactly one version.
3. Make versions match
react and react-dom must be the same version.
Catch it the moment it happens
ErrorBell shows a dot on the page when this error occurs and takes you to the failing line, so you do not have to hunt through the console. Add it to Chrome, it is free.