Invalid hook call. Hooks can only be called inside of the body of a function component
#151Error
Error: 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
Why it happens, and why this works
React tracks hooks through the one React instance that is rendering the component. A hook called outside a component (or in a plain helper function, a class, or a callback), or a second copy of React loaded by a dependency or a linked package, means the hook talks to a React that isn't rendering anything.
Fix
1. Make sure the hook is called at the top level of a function component or a custom hook whose name starts with use, not inside a loop, condition, event handler or plain function. 2. Check for duplicate React: npm ls react (or pnpm why react). Every entry should be the same version. Fix with npm dedupe, or make the library list react as a peerDependency, not a dependency. 3. Keep react and react-dom on the same version. 4. With npm link / a local package, the package's own node_modules/react is the second copy: alias react to the app's copy (resolve.alias / dedupe: ["react", "react-dom"] in Vite) or remove it.
Did it work?
No reports yetRelated fixes
Error: Functions cannot be passed directly to Client Components unless you explicitly expose it by marking it with "use server"
#150Error: Functions cannot be passed directly to Client Components unless you explicitly expose it by marking it with "use server". Or maybe you meant to call this function rather than return it.
SyntaxError: await is only valid in async functions and the top level bodies of modules
#28SyntaxError: await is only valid in async functions and the top level bodies of modules
Next.js: You're importing a component that needs useState. It only works in a Client Component
#110You're importing a component that needs `useState`. This React Hook only works in a Client Component. To fix, mark the file (or its parent) with the `"use client"` directive.