Invalid hook call. Hooks can only be called inside of the body of a function component
Most likely cause: Mismatching versions of React and the renderer such as React DOM
Try first: Inspect the component code
Likely causes
- Mismatching versions of React and the renderer such as React DOMmost likely
- Calling a hook outside the body of a function component, for example inside a loop, condition or nested functionpossible
- Multiple copies of React installed in the projectless likely
How to fix it
Inspect the component codeSafe
Open the file where the error appears and confirm every hook call is at the top level inside the function component with no wrapping conditions or loops.
Compare React package versionsSafe
Open package.json and ensure the versions listed for react and react-dom are identical.
Reinstall dependenciesChanges settings
Delete the node_modules folder and package-lock.json then run npm install to remove duplicate React copies.
⚠ This will remove and rebuild all installed packages; back up package.json first if you have custom changes.
If that didn't fix it
Run npm ls react in the project folder to list all React installations and remove any duplicates.
When to see a technician
Consult an experienced React developer if the error continues after version checks and code review.
Quick answers
- What does Invalid hook call. Hooks can only be called inside of the body of a function component mean?
- This React error occurs when a hook is used outside a function component or when there are conflicting React versions, preventing proper hook execution.
- What causes Invalid hook call. Hooks can only be called inside of the body of a function component?
- Mismatching versions of React and the renderer such as React DOM. (most likely) Calling a hook outside the body of a function component, for example inside a loop, condition or nested function. (possible) Multiple copies of React installed in the project. (less likely)
- How do I fix Invalid hook call. Hooks can only be called inside of the body of a function component?
- Try these in order, safest first: 1. Inspect the component code. 2. Compare React package versions. 3. Reinstall dependencies.
- What if that didn't fix it?
- Run npm ls react in the project folder to list all React installations and remove any duplicates.
- When should I see a technician?
- Consult an experienced React developer if the error continues after version checks and code review.