Skip to content
Advertisement

Uncaught TypeError: Cannot destructure property when using React context

Hello guys so i tried to make global state so the other page can use the state. The problem is i got an error that says:

JavaScript

Im doing this because i want the email from user after logged in and pass them to another page so that i can display the logged in user.

App.js:

JavaScript

Login.js

JavaScript

Advertisement

Answer

EmailUser context works only with the components that are children of EmailUser.Provider, and it doesn’t seem to be the case for Login component. An easy way is to create a separate component, in some EmailUserProvider.js, like so:

JavaScript

And make it wrap all the components that would consume it. If I assume all my components and routes are rendered in App and want the context to be global, I would do so:

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