Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

TypeError: Cannot destructure property of useContext(...) as it is undefined

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

  • Applies to React 19, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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);
  2. 2Match what you see, then jump to its fix

    1. 1The log prints undefined, or null if you wrote createContext(null)The component is rendered outside the providerFix
    2. 2The provider is present in the tree, but the context value is still undefined, and React warns in the consoleThe provider has no value prop, or the prop has another nameFix

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' src

2Fix

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

Did this fix it?

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)?[ >]' src

2Fix

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

Did this fix it?

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:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.