TypeError: Cannot read properties of null (reading 'useContext') in React
TypeError: 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.
Fix it fast
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-dom2Match what you see, then jump to its fix
- 1
npm ls react lists react under a library as well as at the topTwo copies of React in node_modulesFix - 2
The nested react sits under the library in npm ls and dedupe leaves itA library lists react as a dependency, not a peer dependencyFix - 3
npm ls shows react and react-dom with different versionsreact and react-dom are different versionsFix - 4
You develop a library locally and its own node_modules has reactYou develop a library locally and link it into your appFix
- 1
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 react2Fix
Let npm move shared dependencies further up the tree so that packages can share one copy.
npm dedupe
npm ls react3Verify
npm ls react now shows a single version.
npm ls reactConfirmed by react.dev
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 react2Fix
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 reactConfirmed by docs.npmjs.com
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-dom2Fix
Install both at the same version.
npm install react@latest react-dom@latest3Verify
Both commands show the same version, and the page renders.
npm ls react
npm ls react-domConfirmed by react.dev
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/react2Fix
From the library folder, link the app's copy of react into it, as the React docs describe.
npm link ../my-app/node_modules/react3Verify
Restart the dev server. npm ls react shows one copy.
npm ls reactConfirmed by react.dev
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
Related fixes
Web apps
React says a Hook was called where React is not rendering a component
Uncaught TypeError: Cannot read properties of null (reading 'useState')
A Hook only works while React is rendering a function component or a custom Hook.
4 causes, each with a fixWeb apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
AxiosError Network Error with code ERR_NETWORK in a React app
Network Error
ERR_NETWORK means the request produced no readable response.
4 causes, each with a fixWeb apps
Axios get works with a URL string but ignores the object passed as the second argument
The server log shows a GET with no query string
axios.get takes a URL and an optional config object.
3 causes, each with a fixWeb apps
Can't resolve '../axios' in a React project
Module not found: Error: Can't resolve '../axios'
A path that starts with ./ or ../ is a file path, not the axios package.
3 causes, each with a fixWeb apps
Axios returns 401 from a React app but the login works elsewhere
Error: Request failed with status code 401
A 401 means the server received the request without valid credentials.
3 causes, each with a fix