The invalid hook call warning means a Hook was called somewhere React does not allow. The usual causes are breaking the Rules of Hooks, mismatched React versions, or two copies of React in the same bundle. Each has a distinct fix.
The three causes
React throws "Invalid hook call. Hooks can only be called inside of the body of a function component" when it detects a Hook in an unsupported context.
| Cause | What to check |
|---|---|
| Breaking the Rules of Hooks | a Hook inside a loop, condition, or nested function |
| Mismatched React versions | react and react-dom on different versions |
| Duplicate React copies | two copies of react in the bundle |
All three produce the same message, so the fix depends on which one applies. The call site is the fastest thing to check.
Fix the Rules of Hooks
Hooks must run at the top level of a function component or a custom Hook. A Hook inside a condition breaks that rule.
function Message({ warning }) {
if (warning) {
const [seen, setSeen] = useState(false);
}
return <p>Message</p>;
}The number of Hook calls changes between renders, which React forbids. Move the Hook to the top level and use the condition inside it.
function Message({ warning }) {
const [seen, setSeen] = useState(false);
if (warning && !seen) {
setSeen(true);
}
return <p>Message</p>;
}Now the Hook always runs, and the condition only decides what happens next. The fix is the same for loops, nested functions, and early returns: move the Hook above them. See the Rules of Hooks explained for the full list of forbidden call sites.
Align your versions
React and React DOM must be the same version, because the Hook dispatcher lives in one copy of React. Open the package file and confirm both resolve to the same version.
Installing or upgrading them together, such as with the same version range, avoids a mismatch where one package still points at an older copy. A mismatch is easy to miss because both packages usually install as react and react-dom without a version check.
Remove the duplicate React
If the same app bundles two copies of React, one copy owns the state and the other reads a different dispatcher, which triggers the warning. This often comes from a dependency that bundles its own React.
Check the resolved tree for duplicate entries and deduplicate, or add a resolution so every package points at one React version. This one often appears after adding a library that bundles its own copy. See common React setup errors and how to fix them for the dependency side of this.
Rune AI
Key Insights
- Hooks only run at the top level of components or custom Hooks.
- Do not call Hooks in loops, conditions, or nested functions.
- Keep React and React DOM on the same version.
- Remove duplicate copies of React from the bundle.
- eslint-plugin-react-hooks catches rule breaks.
Frequently Asked Questions
What are the three common causes of invalid hook call?
Does this warning mean my Hook is broken?
Conclusion
The invalid hook call warning means a Hook ran outside a component or custom Hook, or React itself is duplicated. Fix the call site, align versions, and remove duplicate React.
More in this topic
How to Build a Dropdown Menu in React
Build a React dropdown menu with the ARIA menu button pattern. Handle open and close, keyboard arrows, and clicks outside the menu.
How to Animate React Components with Motion
Animate React components with the Motion library. Set up motion, add enter, hover, and exit animations, and respect reduced motion.
Headless UI Components Explained: Logic Without Locked Styling
Understand headless UI components and how libraries like Radix give you unstyled, accessible behavior that you style yourself.