Cannot read properties of null: ReactCurrentDispatcher or useState
TypeError: 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.
Fix it fast
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-dom2Match what you see, then jump to its fix
- 1
npm ls react shows the library carrying its own nested reactTwo copies of React in the dependency treeFix - 2
npm ls shows react and react-dom on different major versionsreact and react-dom versions do not matchFix - 3
The stack trace points into a handler, a condition, a loop or a class componentA hook is called where React does not allow itFix
- 1
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 react2Fix
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/react3Verify
Reinstall, restart the dev server and see only one react in the tree.
rm -rf node_modules package-lock.json && npm install
npm ls reactConfirmed by react.dev
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-dom2Fix
Install both at the same version.
npm install react@latest react-dom@latest3Verify
Both packages report the same version and the page renders.
npm ls react react-domConfirmed by react.dev
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-hooks2Fix
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
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:
- 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 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 fixWeb apps
An axios call never finishes when the API server stops answering
code: 'ECONNABORTED'
Axios has no timeout unless you give it one: the default is 0, which means wait forever.
3 causes, each with a fix