Error Encyclopedia

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

1

Calling a hook inside a condition (if statement)

2

Calling a hook inside a loop (for, while)

3

Calling a hook inside a callback or event handler

4

Calling a hook inside a class component

5

Multiple versions of React in the same application (duplicate react packages)

6

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: