Maaz Zindani

Loading the index…

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

TypeError: Cannot read properties of null (reading 'useContext') in React

ErrorTypeError: Cannot read properties of null (reading 'useContext')

Short answer

This error usually means a library calls hooks on a different copy of React than the one react-dom renders with. Run npm ls react to see how many copies exist, make the library use your single copy with dedupe or an override, and check react and react-dom are the same version.

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

Fix it fast

  1. 1Find out which cause you have

    Count the React copies in your dependency tree. One path means this is not your problem; two or more is the problem.

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

    1. 1npm ls react lists react under a library as well as at the topTwo copies of React in node_modulesFix
    2. 2The nested react sits under the library in npm ls and dedupe leaves itA library lists react as a dependency, not a peer dependencyFix
    3. 3npm ls shows react and react-dom with different versionsreact and react-dom are different versionsFix
    4. 4You develop a library locally and its own node_modules has reactYou develop a library locally and link it into your appFix

What does the error look like?

  • TypeError: Cannot read properties of null (reading 'useContext')
  • Hooks can only be called inside the body of a function component.

Cause 1 of 4

Two copies of React in node_modules

1Check

Hooks only work when the react your components import and the react inside react-dom are the same module. A nested second copy breaks that.

npm ls react

2Fix

Let npm move shared dependencies further up the tree so that packages can share one copy.

npm dedupe
npm ls react

3Verify

npm ls react now shows a single version.

npm ls react

Confirmed by react.dev

Did this fix it?

Cause 2 of 4

A library lists react as a dependency, not a peer dependency

1Check

React's own page names this as a common cause. A library that needs react but does not declare it as a peer can end up with its own copy.

npm ls react

2Fix

Update the library to a release that treats react as a peer. Until then, force one version for the whole tree with an override in the root package.json, using your own react spec.

{
  "dependencies": {
    "react": "^19.0.0"
  },
  "overrides": {
    "react": "$react"
  }
}

3Verify

Run npm install, then npm ls react: one version across the tree.

npm install
npm ls react

Confirmed by docs.npmjs.com

Did this fix it?

Cause 3 of 4

react and react-dom are different versions

1Check

React's page lists mismatched React and React DOM versions as a cause of this family of errors.

npm ls react
npm ls react-dom

2Fix

Install both at the same version.

npm install react@latest react-dom@latest

3Verify

Both commands show the same version, and the page renders.

npm ls react
npm ls react-dom

Confirmed by react.dev

Did this fix it?

Cause 4 of 4

You develop a library locally and link it into your app

1Check

A linked library resolves react from its own folder, which gives your app two copies.

ls ../my-lib/node_modules/react

2Fix

From the library folder, link the app's copy of react into it, as the React docs describe.

npm link ../my-app/node_modules/react

3Verify

Restart the dev server. npm ls react shows one copy.

npm ls react

Confirmed by react.dev

Did this fix it?

How do I stop it coming back?

  • Publish libraries with react in peerDependencies, never in dependencies.
  • Run npm ls react after adding any UI library.
  • Keep react and react-dom on one version, updated together.

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.