How to fix Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons: 1. You might have mismatching versions of React and the renderer (such as React DOM) 2. You might be breaking the Rules of Hooks 3. You might have more than one copy of React in the same app in React

ReactINTERMEDIATEHIGHUpdated Dec 8, 2025· 155 views

This error occurs when React Hooks are called outside the body of a function component or custom Hook, violating one of React's fundamental rules. The error typically happens due to version mismatches, calling Hooks in class components, or placing Hooks inside loops, conditions, or nested functions.

Quick fix

Fixes Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons: 1. You might have mismatching versions of React and the renderer (such as React DOM) 2. You might be breaking the Rules of Hooks 3. You might have more than one copy of React in the same app

function MyComponent() {
  if (condition) {
    const [state, setState] = useState(0); // ❌ Error!
  }
  return <div>Content</div>;
}
  1. Verify Hooks are called at the top level of function components
  2. Move Hooks out of event handlers and helper functions
  3. Check for React version mismatches
  4. Detect and resolve duplicate React installations
react — error
function MyComponent() {
if (condition) {
const [state, setState] = useState(0); // ❌ Error!
}
return <div>Content</div>;
}
Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
1. You might have mismatching versions of React and the renderer (such as React DOM)
2. You might be breaking the Rules of Hooks
3. You might have more than one copy of React in the same app

What this error means

This error is React's way of enforcing the Rules of Hooks, which are essential for React to maintain component state correctly across renders. Hooks like useState, useEffect, and useContext rely on being called in the exact same order every time a component renders. When React detects a Hook being called outside a function component or custom Hook, or in a place where the call order might change (like inside a loop or condition), it throws this error to prevent unpredictable behavior. The error occurs because React internally uses the order of Hook calls to associate each Hook with its corresponding state. If Hooks are called conditionally or outside function components, React cannot guarantee this order will remain consistent, which would break the state management system and lead to bugs where components don't update correctly or lose their state between renders. Understanding this error is crucial because it represents one of the most fundamental constraints in modern React development. Unlike class components where lifecycle methods could be called anywhere, Hooks have strict placement rules that must be followed to ensure React's rendering system works correctly.

Typical symptoms

  • Application crashes immediately when a component renders that calls a Hook improperly
  • Error message explicitly states "Hooks can only be called inside of the body of a function component"
  • React Developer Tools shows warnings about invalid Hook calls
  • Component state behaves unpredictably or doesn't update when expected
  • Error occurs after upgrading React versions or adding new dependencies
  • Build succeeds but runtime error appears in browser console
  • Error happens when calling useState, useEffect, or other Hooks inside event handlers or helper functions

Common causes

  1. Calling Hooks inside class components instead of function components
  2. Placing Hook calls inside loops, conditional statements, or nested functions
  3. Mismatched versions between react and react-dom packages in package.json
  4. Multiple copies of React installed in node_modules (common with npm link or monorepo setups)
  5. Calling Hooks inside event handlers, callbacks, or regular JavaScript functions
  6. Calling React components as regular functions (e.g., MyComponent() instead of <MyComponent />)
  7. Attempting to use Hooks in a function that isn't a component or custom Hook (must start with "use" prefix)
  8. Breaking Hook rules after a conditional early return in a component

How to fix "Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons: 1. You might have mismatching versions of React and the renderer (such as React DOM) 2. You might be breaking the Rules of Hooks 3. You might have more than one copy of React in the same app"

1Verify Hooks are called at the top level of function components

Review your code to ensure all Hook calls are at the top level of your function component, before any early returns, loops, or conditions. Hooks must be called in the exact same order on every render.

Bad - Hook inside condition:

javascript
function MyComponent() {
  if (condition) {
    const [state, setState] = useState(0); // ❌ Error!
  }
  return <div>Content</div>;
}

Good - Hook at top level:

javascript
function MyComponent() {
  const [state, setState] = useState(0); // ✓ Correct
  
  if (condition) {
    // Use state here
  }
  return <div>Content</div>;
}
2Move Hooks out of event handlers and helper functions

If you're calling Hooks inside event handlers or utility functions, move the Hook call to the component level and pass the state or values down instead.

Bad - Hook in event handler:

javascript
function MyComponent() {
  const handleClick = () => {
    const [count, setCount] = useState(0); // ❌ Error!
    setCount(count + 1);
  };
  
  return <button onClick={handleClick}>Click</button>;
}

Good - Hook at component level:

javascript
function MyComponent() {
  const [count, setCount] = useState(0); // ✓ Correct
  
  const handleClick = () => {
    setCount(count + 1);
  };
  
  return <button onClick={handleClick}>Click {count}</button>;
}
3Check for React version mismatches

Verify that your react and react-dom versions match and are both 16.8.0 or higher (Hooks were introduced in 16.8.0).

bash
npm ls react react-dom

If versions don't match or are below 16.8.0, update them:

bash
npm install react@latest react-dom@latest

Or specify matching versions:

bash
npm install react@18.2.0 react-dom@18.2.0

After updating, clear your cache and reinstall:

bash
rm -rf node_modules package-lock.json
npm install
4Detect and resolve duplicate React installations

Multiple React copies in node_modules can cause this error. Check for duplicates:

bash
npm ls react

If you see multiple versions listed, this is your problem. Common causes:

For npm link or local packages:

bash
# In your linked package
npm link react react-dom

# In your main project
npm link

For monorepos or workspaces:
Add to your package.json:

json
{
  "resolutions": {
    "react": "18.2.0",
    "react-dom": "18.2.0"
  }
}

Then reinstall:

bash
rm -rf node_modules package-lock.json
npm install
5Convert class components to function components

If you're trying to use Hooks in a class component, you must convert it to a function component first. Hooks cannot be used in classes.

Before - Class component:

javascript
class MyComponent extends React.Component {
  render() {
    const [count, setCount] = useState(0); // ❌ Error!
    return <div>{count}</div>;
  }
}

After - Function component:

javascript
function MyComponent() {
  const [count, setCount] = useState(0); // ✓ Correct
  return <div>{count}</div>;
}

Or with arrow function:

javascript
const MyComponent = () => {
  const [count, setCount] = useState(0);
  return <div>{count}</div>;
};
6Create custom Hooks for reusable logic

If you need to share Hook logic between components, extract it into a custom Hook (a function starting with "use").

Bad - Hook in regular function:

javascript
function getWindowWidth() {
  const [width, setWidth] = useState(window.innerWidth); // ❌ Error!
  return width;
}

Good - Custom Hook:

javascript
function useWindowWidth() {
  const [width, setWidth] = useState(window.innerWidth); // ✓ Correct
  
  useEffect(() => {
    const handleResize = () => setWidth(window.innerWidth);
    window.addEventListener('resize', handleResize);
    return () => window.removeEventListener('resize', handleResize);
  }, []);
  
  return width;
}

// Usage in component
function MyComponent() {
  const width = useWindowWidth();
  return <div>Width: {width}</div>;
}
7Install and configure ESLint plugin for Hooks

Use the official ESLint plugin to catch Hook rule violations during development:

bash
npm install eslint-plugin-react-hooks --save-dev

Add to your .eslintrc configuration:

json
{
  "plugins": ["react-hooks"],
  "rules": {
    "react-hooks/rules-of-hooks": "error",
    "react-hooks/exhaustive-deps": "warn"
  }
}

This will show errors in your editor when you violate Hook rules, catching issues before runtime.

Advanced notes

Component vs. Function Naming: React determines if a function is a component by its name. Component names must start with a capital letter (e.g., MyComponent), while custom Hooks must start with "use" (e.g., useCustomHook). Regular helper functions should use lowercase. If you name a component with a lowercase letter, React may not recognize it as a component and Hook calls inside it will fail.

Webpack Module Resolution: In complex build setups with webpack, you might have duplicate React modules even if your package.json is correct. Use webpack's resolve.alias to force a single React instance:

javascript
// webpack.config.js
module.exports = {
  resolve: {
    alias: {
      react: path.resolve('./node_modules/react'),
      'react-dom': path.resolve('./node_modules/react-dom')
    }
  }
};

Testing Environments: This error can appear in test files when using Jest or other testing frameworks. Ensure your test setup correctly mocks React and doesn't create multiple instances. Use the same React version in your test dependencies as in your application dependencies.

Server Components (Next.js 13+): In Next.js App Router with React Server Components, you cannot use Hooks in Server Components at all. Add "use client" directive at the top of files that need Hooks. This is a different constraint from the Hook rules but can produce similar error messages.

Performance Implications: While it might be tempting to conditionally call Hooks to optimize performance, this breaks React's state tracking. Instead, always call the Hook but conditionally use its result, or conditionally execute effects inside useEffect's callback function.

Debugging Tips: When the error message doesn't clearly indicate where the violation occurred, add console.logs at the start of your components to identify which component is rendering when the error happens. React DevTools' component stack trace can also help pinpoint the problematic component.

Frequently asked questions

What does "Invalid hook call. Hooks can only be called inside of the body of a fu" mean?
This error is React's way of enforcing the Rules of Hooks, which are essential for React to maintain component state correctly across renders. Hooks like useState, useEffect, and useContext rely on being called in the exact same order every time a component renders. When React detects a Hook being called outside a function component or custom Hook, or in a place where the call order might change (like inside a loop or condition), it throws this error to prevent unpredictable behavior.
How do I fix "Invalid hook call. Hooks can only be called inside of the body of a fu"?
This error occurs when React Hooks are called outside the body of a function component or custom Hook, violating one of React's fundamental rules. The error typically happens due to version mismatches, calling Hooks in class components, or placing Hooks inside loops, conditions, or nested functions.
What causes "Invalid hook call. Hooks can only be called inside of the body of a fu"?
Calling Hooks inside class components instead of function components Placing Hook calls inside loops, conditional statements, or nested functions Mismatched versions between react and react-dom packages in package.json
Is the "Invalid hook call. Hooks can only be called inside of the body of a fu" error serious?
Yes. This is a high-severity error that usually blocks work until it is fixed, but the steps above resolve it.

Official resources & further reading