Skip to content
Advertisement

Uncaught TypeError: Cannot destructure property ‘xxx’ of ‘useAuth(…)’ as it is undefined

Working with React Context and Hooks I am creating a provider called AuthProvider and from the useAuth Hook I call that context to work with it. I have a component called Login in which I call my Hook to access the AuthProvider as follows:

JavaScript

In AuthContext I have the variable “hello” that I pass to the children of my context.

AuthProvider:

JavaScript

UseAuth Hook:

JavaScript

Context Routes

And this is the error:

JavaScript

Advertisement

Answer

The problem is that the component you use context but was not wrapped under a context provider

To fix, wrap the component or the root component that has the component as a children under a context provider

JavaScript

Demo

Edit pedantic-stonebraker-myzk4e

References

https://reactjs.org/docs/context.html

https://reactjs.org/docs/hooks-reference.html#usecontext

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement