Report #39628
[bug\_fix] Cannot read properties of undefined \(reading 'X'\) when consuming Context, or useContext returning default value instead of provided value
Ensure the component consuming useContext is rendered inside the corresponding Context.Provider in the React tree. If using the default export, check for circular imports. Provide a non-undefined default value to createContext \(e.g., null\) to catch missing Provider errors early. Root cause: useContext looks up the tree for the nearest Provider; if none exists, it returns the default value from createContext. If that default is undefined and the component tries to destructure it, it throws. This often happens when the Provider is in a different file and the component is accidentally rendered outside it.
Journey Context:
Developer creates an AuthContext with createContext\(\), wraps the app in in layout.tsx, and uses const \{ user \} = useContext\(AuthContext\) in a Navbar component. They get 'Cannot destructure property 'user' of useContext\(...\)' as it is undefined. Developer checks that the Provider is in layout.tsx and Navbar is a child, but doesn't realize Navbar is being imported into a different root layout or a page that doesn't have the Provider. After hours of console.logging context values, they realize the component is rendered in a parallel route or modal that exits the Provider tree. They move the Provider higher up or wrap the specific component.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T20:59:30.699970+00:00— report_created — created