TypeError: Cannot destructure property of useContext(...) as it is undefined
TypeError: Cannot destructure property 'user' of '(0 , react__WEBPACK_IMPORTED_MODULE_0__.useContext)(...)' as it is undefined.Short answer
useContext returns what the nearest provider above passes as value. With no provider, it returns the default given to createContext, which is undefined when you pass none. Destructuring that throws. Wrap the component in the provider, pass a value prop, and throw a clear error from a custom hook.
Fix it fast
1Find out which cause you have
Log what useContext really returns in the failing component, and open the browser console for a React warning about the provider.
const ctx = useContext(AuthContext); console.log('auth context:', ctx);2Match what you see, then jump to its fix
What does the error look like?
TypeError: Cannot destructure property 'user' of '(0 , react__WEBPACK_IMPORTED_MODULE_0__.useContext)(...)' as it is undefined.The value is null when the context was created with createContext(null)
Cause 1 of 2
The component is rendered outside the provider
1Check
If no matching provider exists above the component, useContext returns the default value you gave createContext. Calling createContext() with no argument means that default is undefined. Look at where the provider sits in the tree compared with the failing component, for example a provider placed inside one page while a header outside it reads the context.
grep -rn -E 'AuthContext|createContext' src2Fix
Move the provider higher, so it wraps every component that reads the context. Then make a missing provider fail loudly with a small hook.
const AuthContext = createContext(null);
export function useAuth() {
const value = useContext(AuthContext);
if (value === null) {
throw new Error('useAuth must be used inside AuthProvider');
}
return value;
}3Verify
Reload the page. The component gets an object, or the error now names the missing provider.
console.log(useAuth());Confirmed by react.dev
Cause 2 of 2
The provider has no value prop, or the prop has another name
1Check
Leaving out value is the same as passing value={undefined}. A prop with another name, such as user or data, is ignored. The default value is not used either, because a provider is present.
grep -rn -E 'AuthContext(\.Provider)?[ >]' src2Fix
Pass the object as a prop named value.
<AuthContext value={{ user, login, logout }}>
{children}
</AuthContext>3Verify
The log now shows the object, and destructuring user works.
console.log(useContext(AuthContext));Confirmed by react.dev
How do I stop it coming back?
- Export a custom hook per context and never call useContext directly in components.
- Pick null as the default when no real default makes sense, and check for it in the hook.
- Keep each provider in its own file with a short test that renders a child inside it.
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