react hook_error ai_generated true

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

ID: react/cannot-read-null-usecontext

Also available as: JSON · Markdown
90%Fix Rate
90%Confidence
50Evidence
2023-01-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
18 active

Root Cause

React hooks internals are null — usually multiple React copies or calling hooks outside component.

generic

Workarounds

  1. 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

  2. 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 fail

    Sources: https://react.dev/warnings/invalid-hook-call-warning

  3. 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:

  1. Downgrade React version 75% fail

    Usually not a version issue — it's duplicate React installations

  2. Wrap everything in a new React.createContext 80% fail

    The issue is React internals, not your context

Error Chain

Frequently confused with: