Skip to content
>_VibeUnstuck

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

Last verified:

The fix

React had no Hooks dispatcher when this function ran. The hook is outside a component, the Rules of Hooks were broken, or the page has two copies of React.

  1. 1

    Read the first line, then where the hook is

    If the message says the hook only works in a Client Component, stop and use that page. For Invalid hook call, open the component in the stack. A call inside if, after return, inside onClick, or inside a plain function is the Rules of Hooks case. Move every hook above the first conditional return. A helper that calls a hook has to be a custom Hook: its name starts with use, and you call it from a component at the top level. Do not add an extra useState to balance a count.

  2. 2

    If the order of Hooks changed, the condition is the bug

    React has detected a change in the order of Hooks called by Profile means this render ran a different sequence than the last one. Rendered more hooks than during the previous render is the same mistake. Pull the hook out of the branch. Compute the branch after the hooks. Do not call the hook inside useEffect to "get it out of the body." The rules page forbids that too.

    function Profile({ user }: { user: User | null }) {
      const [open, setOpen] = useState(false);
      if (!user) return <p>Signed out</p>;
      return <button onClick={() => setOpen(true)}>{user.email}</button>;
    }
  3. 3

    Count the React copies

    From the app directory, run npm ls react and npm ls react-dom. One version of each, and the same major, is what you want. A nested react@… under some other package is the second copy. That package should take React from your app (peerDependency), not ship its own. Do not delete react from your package.json to dedupe it. Do not install a second React in src. Mismatching majors of react and react-dom is the other line on the invalid-hook-call page. Align them and reinstall.

  4. 4

    Confirm the stack is a component render

    The hook has to run while React is rendering a function component. Calling it from a route loader, a plain onClick, or a module scope throws invalid hook call even when the file starts with "use client". After the move, reload once. The order warning should be gone on the second render, not only the first.

Why it happens

The thrown message starts Invalid hook call. Hooks can only be called inside of the body of a function component. React's invalid-hook-call page lists the three reasons: you might be breaking the Rules of Hooks, you might have mismatching versions of React and the renderer (such as React DOM), or you might have more than one copy of React in the same app. The dispatcher is null, so useState does not know which component it belongs to.

The Rules of Hooks page says to call Hooks only at the top level of a function component or a custom Hook, before any early return. Not inside a condition, a loop, a nested function, an event handler, a class, a try/catch, or the function you pass to useMemo, useEffect, or useReducer. Generated code calls useState inside a helper whose name does not start with use, or under if (user). A conditional hook often survives the first render and then throws React has detected a change in the order of Hooks called by ComponentName or Rendered more hooks than during the previous render. Those sentences are the rules, not a second copy of React.

Next.js has a different sentence when a Server Component imports a hook: useState only works in a Client Component. That page is the "use client" fix. Do not treat it as an invalid hook call. Two copies of React show up when a nested package bundles its own react, or when react and react-dom resolve to different versions. The invalid-hook-call page says to run npm ls react and npm ls react-dom.

The next error you'll probably hit

Once this is fixed, the usual follow-up is Too many re-renders. React limits the number of renders to prevent an infinite loop.. A state update ran during render and immediately scheduled another render. React stopped it. A production build reports this as minified React error #301.

Prompt to paste back into your AI tool

Paste this above the error.

React throws: Invalid hook call. Hooks can only be called inside of the body of a function component. If the text is instead "React has detected a change in the order of Hooks" or "Rendered more hooks than during the previous render" or "useState only works in a Client Component", quote that sentence. The component is:
<paste the component>

npm ls react output, if you have it:
<paste>

Do not add "use client" to the root layout unless the message says Client Component. Do not add a dummy useState to make the hook count match. Do not call the hook inside useEffect or inside the click handler. Do not install a second copy of react. Do not delete react from package.json.

1. If the message says Client Component, stop and follow that fix: a small client file, not this one.
2. Move every hook to the top of the function component or custom Hook, above any return or condition.
3. If npm ls react shows more than one copy, or react and react-dom disagree, name the package and align them. Do not patch node_modules by hand.
4. Change only that component, unless a dependency is the second React.

Common questions

+The message is useState only works in a Client Component. Is that an invalid hook call?

No. Next.js printed that because a Server Component imported the hook. Add "use client" on the small interactive file. Invalid hook call means the dispatcher was null: the call site is not a component render, the Rules of Hooks were broken, or there are two Reacts.

+It worked on the first paint and then says the order of Hooks changed. Why?

A hook is behind a condition or after an early return, so this render executed a different list than the previous one. React has detected a change in the order of Hooks called by that component. Move the hook above the branch. Another useState does not repair the order.

+npm ls react shows a second copy under one library. What do I change?

The app should have a single React. The invalid-hook-call page tells you to look for that duplicate. The library should depend on React as a peer, not bundle its own. Do not npm install react inside that library's folder from the app, and do not remove your own dependency.

Related errors