Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

Cannot read properties of null: ReactCurrentDispatcher or useState

ErrorTypeError: Cannot read properties of undefined (reading 'ReactCurrentDispatcher')

Short answer

This error means React's hooks dispatcher is missing when a hook runs. The causes are three: two copies of React installed, react and react-dom on different versions, or a hook called where hooks are not allowed. Run npm ls react first to see whether more than one copy exists.

  • Applies to React 19, Node.js 22, npm 10
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    List every copy of react in the dependency tree. More than one line with a different path or version points to a duplicate.

    npm ls react react-dom
  2. 2Match what you see, then jump to its fix

    1. 1npm ls react shows the library carrying its own nested reactTwo copies of React in the dependency treeFix
    2. 2npm ls shows react and react-dom on different major versionsreact and react-dom versions do not matchFix
    3. 3The stack trace points into a handler, a condition, a loop or a class componentA hook is called where React does not allow itFix

What does the error look like?

  • TypeError: Cannot read properties of undefined (reading 'ReactCurrentDispatcher')
  • Invalid hook call. Hooks can only be called inside of the body of a function component.
  • TypeError: Cannot read properties of null (reading 'useState')

Cause 1 of 3

Two copies of React in the dependency tree

1Check

The react your code imports must resolve to the same module as the react inside react-dom. A library listing react under dependencies instead of peerDependencies is a common source.

npm ls react
npm explain react

2Fix

Fix the declaration in the offending package, or force one copy with an override in your package.json. If you use npm link for a local library, link the library to the app's copy of React.

// package.json
"overrides": { "react": "$react" }

# local library linked into an app
npm link ../myapp/node_modules/react

3Verify

Reinstall, restart the dev server and see only one react in the tree.

rm -rf node_modules package-lock.json && npm install
npm ls react

Confirmed by react.dev

Did this fix it?

Cause 2 of 3

react and react-dom versions do not match

1Check

Hooks need react-dom 16.8.0 or later, and both packages should be kept on the same release.

npm ls react react-dom

2Fix

Install both at the same version.

npm install react@latest react-dom@latest

3Verify

Both packages report the same version and the page renders.

npm ls react react-dom

Confirmed by react.dev

Did this fix it?

Cause 3 of 3

A hook is called where React does not allow it

1Check

Hooks must run at the top level of a function component or custom hook, before any early return.

npm install -D eslint-plugin-react-hooks

2Fix

Move the hook to the component body, and rewrite class components as function components if they need hooks. Enable eslint-plugin-react-hooks to catch it automatically.

function Bad({ cond }) {
  if (cond) return null;
  const theme = useContext(ThemeContext); // wrong: after a return
}

function Good({ cond }) {
  const theme = useContext(ThemeContext); // right: top of the component
  if (cond) return null;
}

3Verify

Run the linter, then load the page.

npx eslint src/

Confirmed by react.dev

Did this fix it?

How do I stop it coming back?

  • Run npm ls react after adding any UI library.
  • Require libraries to declare react as a peerDependency.
  • Keep eslint-plugin-react-hooks on in CI.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.