Skip to content

Error: Invalid hook call

Error message

                Invalid hook call. Hooks can only be called inside of the body of a function component.
              

Quick summary

You called a React Hook (useState, useEffect, etc.) outside a function component or another Hook, which violates the Rules of Hooks.

Why this happens

  • Called a Hook inside a regular function (not a component or custom Hook)

  • Called a Hook inside a class component method

  • Called a Hook inside a loop, condition, or nested function

  • Mismatched versions of React and React DOM

Minimal example

✗ Broken code
// Hook called in a regular function
function helper() {
  const [count, setCount] = useState(0); // Invalid!
  return count;
}
✓ Fixed code
// Hook called in a custom Hook (name starts with "use")
function useCount() {
  const [count, setCount] = useState(0);
  return count;
}

React detects Hooks by function name. Renaming helper to useCount tells React this is a custom Hook, making the useState call valid.

How to diagnose

  • Check that the Hook is called at the top level of a function component

  • Verify the function name starts with a capital letter (component) or 'use' (custom Hook)

  • Check that you're not inside a class component

  • Run npm ls react and npm ls react-dom to check for version mismatches

How to fix

  • Move the Hook call to the top level of a function component

  • Rename the function to start with 'use' if it's a custom Hook

  • Convert the class component to a function component

  • Ensure React and React DOM are the same version

How to prevent

  • Install eslint-plugin-react-hooks and enable the rules-of-hooks rule

  • Always name custom Hooks with the 'use' prefix

Related Resources

Related Glossary

Related Lessons

  • State

    Learn the Rules of Hooks.

← Back to language