Skip to content

React errorReact: “Invalid hook call. Hooks can only be called inside of the body of a function component”

The app crashes with Invalid hook call, often right after adding a package, moving code into a helper, or linking a local library. React gives three causes: a Hook called in the wrong place, react and react-dom on versions that do not match, or two copies of React in the same app. The first is a code fix; the other two are in your dependencies.

Short answer

Check where the Hook is called first: it must be at the top level of a function component or a custom Hook, never inside a condition, loop, event handler, class component or the function you pass to useEffect or useMemo. If the code looks right, run npm ls react. More than one copy of React, often from a library that lists react as a dependency or from npm link, causes the same error. Also check that react and react-dom are the same version. Make every package use one React and restart the dev server.

Why React rejects the Hook call, and the fix

  1. 01

    A Hook is called outside the top level of a component

    “Don’t call Hooks inside loops, conditions, or nested functions. Instead, always use Hooks at the top level of your React function, before any early returns.” React, Invalid Hook Call Warning

    Hooks only work while React renders a function component or a custom Hook. A useState or useContext inside an if, a loop, an onClick handler, after an early return, inside a class component, or inside a function you pass to useEffect, useMemo or useReducer breaks that rule. Move the call to the top of the component, above any early return or condition, and put the condition after the Hook call or inside the effect.

    // breaks: Hook after an early return
    function Profile({ user }) {
      if (!user) return null;
      const theme = useContext(ThemeContext);
    }
    
    // works: Hook first, condition after
    function Profile({ user }) {
      const theme = useContext(ThemeContext);
      if (!user) return null;
    }
  2. 02

    react and react-dom don’t match

    “You might have mismatching versions of React and React DOM.” React, Invalid Hook Call Warning

    react and react-dom (or react-native, or react-test-renderer in tests) must be the same version. Run npm ls react react-dom, then install matching versions of both together.

    npm ls react react-dom
    npm install react@latest react-dom@latest
  3. 03

    Two copies of React in the app

    “For example, maybe a library you’re using incorrectly specifies react as a dependency (rather than a peer dependency).” React, Invalid Hook Call Warning

    If npm ls react shows more than one copy, often a library lists react as a dependency instead of a peer dependency. Update or replace that library, or force one React with an override in package.json (npm overrides with "$react" need react and react-dom in your own dependencies; Yarn uses resolutions, pnpm uses pnpm.overrides). Then run npm dedupe, delete node_modules and reinstall. For a library linked with npm link, React’s fix is to run npm link ../your-app/node_modules/react from the library folder, so it uses the app’s copy.

    {
      "overrides": {
        "react": "$react",
        "react-dom": "$react-dom"
      }
    }
  4. 04

    Catch it before it ships

    “You can use the eslint-plugin-react-hooks plugin to catch these mistakes.” React, Invalid Hook Call Warning

    The React Hooks ESLint plugin flags a Hook in the wrong place as you type. Turn it on in your linter, so AI-generated or copied code that calls a Hook inside a condition is caught in the editor instead of in production.

Checked against the vendor’s docs on : React: Invalid Hook Call Warning (the three causes and how to check them).

Rather hand it over?

Fixed scope, fixed price, paid upfront. The work happens in writing; no call needed.

Related: React “Too many re-renders” · Next.js hydration error · React “Each child in a list should have a unique key”

Questions

More on this error

Why did it start after I installed a package?

The new package probably brought its own copy of React. Run npm ls react: if two versions show up, the package lists react as a dependency instead of a peer dependency. Update it or force one React with an override.

Can I call a Hook from a regular function?

Only if that function is itself a custom Hook, named with use at the start and called at the top level of a component or another custom Hook. A plain helper function cannot call Hooks.

Can you fix it for me?

If one bug is the whole problem, the App Fix Ticket covers it: a written diagnosis first, then the fix. If the app has more going on, the Launch Check is a written check of the whole app. Each issue found is rated critical, high, medium or low, with where it is and how it was confirmed. No issues worth fixing, no charge. If the Launch Check finds nothing rated medium or higher, Simon refunds the full $450. You keep the report either way. This applies to the first 10 Launch Checks bought after October 30, 2026.

A different error?

Describe it and get a written quote

Send the error and what you were doing. You get a written scope, a fixed price and a timeline; Simon replies by email, usually within one business day.