react
hook_error
ai_generated
true
Invalid hook call. Hooks can only be called inside the body of a function component
ID: react/invalid-hook-call
85%Fix Rate
88%Confidence
410Evidence
2023-01-01First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 18 | active | — | — | — |
Root Cause
Hook called outside a component, in a class component, or with multiple React copies.
genericWorkarounds
-
92% success Ensure hooks are called at the top level of a function component
// Hooks must be at the top level, not inside conditions: function MyComponent() { const [val, setVal] = useState(0); // Always called const data = useMemo(() => compute(val), [val]); // Always called if (condition) return <Fallback />; // Conditional return AFTER hooks } -
85% success Check for duplicate React versions in node_modules
Check for duplicate React versions in node_modules
Sources: https://react.dev/warnings/invalid-hook-call-warning
Dead Ends
Common approaches that don't work:
-
Convert class component to function component just for hooks
55% fail
If the class has complex lifecycle, conversion may introduce bugs
-
Call hooks inside event handlers or callbacks
82% fail
Hooks must be at the top level, not inside conditions or callbacks
Error Chain
Preceded by:
Frequently confused with: