React Invalid Hook Call Error
Fix 'Invalid hook call' error in React. Learn the rules of hooks — only call hooks at the top level and only from React function components.
What Does This Error Mean?
The 'Invalid hook call' error means a React Hook (useState, useEffect, etc.) was called in violation of the Rules of Hooks. Hooks must be called at the top level of a React function component or a custom hook, not inside conditions, loops, or nested functions.
Common Causes
Calling a hook inside a condition (if statement)
Calling a hook inside a loop (for, while)
Calling a hook inside a callback or event handler
Calling a hook inside a class component
Multiple versions of React in the same application (duplicate react packages)
Calling a hook inside a non-React function
How to Fix It
Move hooks to top level
Always call hooks at the top level of your function component, never inside conditions or loops.
function MyComponent() {
// ✅ Top level
const [count, setCount] = useState(0)
const [user, setUser] = useState(null)
// ❌ Inside condition
if (count > 0) {
useEffect(() => {}) // DON'T
}
// ❌ Inside loop
for (let i = 0; i < 5; i++) {
useEffect(() => {}) // DON'T
}
// ✅ Move condition/loop logic inside the hook
useEffect(() => {
if (count > 0) {
// conditional logic here
}
}, [count])
}Check for duplicate React
Multiple React versions can cause hooks to fail. Check your node_modules.
# Check for duplicate React npm ls react # Deduplicate if needed npm dedupe # Or check package.json for mismatched versions # All React-related packages should use the same version
Convert to proper component
Function components and class components use different state patterns.
// ✅ Function component with hooks
function MyComponent() {
const [state, setState] = useState(0)
useEffect(() => { document.title = String(state) }, [state])
return <div>{state}</div>
}
// ❌ Cannot use hooks in class components
class MyComponent extends React.Component {
const [state, setState] = useState(0) // WRONG
}Related Tools
Use these tools to debug and fix this error:
Related Errors
Other common errors in this category:
401 Unauthorized Error
Learn what a 401 Unauthorized error means, common causes, and how to fix authentication failures in your web applications.
403 Forbidden Error
Learn what 403 Forbidden means, how it differs from 401, and how to fix access denied errors in your applications.
404 Not Found Error
Learn what 404 Not Found means, common causes, and how to fix broken links and missing resources on your website or API.
429 Too Many Requests Error
Learn what 429 Too Many Requests means, how rate limiting works, and how to handle or avoid hitting API rate limits.