react
hook_error
ai_generated
true
TypeError: Cannot read properties of null (reading 'useContext')
ID: react/cannot-read-null-usecontext
90%Fix Rate
90%Confidence
50Evidence
2023-01-01First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 18 | active | — | — | — |
Root Cause
React hooks internals are null — usually multiple React copies or calling hooks outside component.
genericWorkarounds
-
95% success Check for duplicate React: npm ls react — should have only one copy
npm ls react npm dedupe
Sources: https://react.dev/warnings/invalid-hook-call-warning#duplicate-react
-
90% success Ensure hooks are called from a React function component, not a regular function
// Hooks only work inside components or custom hooks: function MyComponent() { // Component (uppercase) const ctx = useContext(MyCtx); // OK } // NOT: function helper() { useContext(MyCtx); } // Will failSources: https://react.dev/warnings/invalid-hook-call-warning
-
85% success If using linked packages, ensure they resolve to the same React instance
// In webpack.config.js: resolve: { alias: { react: path.resolve('./node_modules/react') } } // Or in package.json (Yarn): "resolutions": { "react": "18.2.0" }Sources: https://react.dev/warnings/invalid-hook-call-warning#duplicate-react
Dead Ends
Common approaches that don't work:
-
Downgrade React version
75% fail
Usually not a version issue — it's duplicate React installations
-
Wrap everything in a new React.createContext
80% fail
The issue is React internals, not your context
Error Chain
Frequently confused with: