Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

254
Visualizações
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:

import useAuth from '../hooks/useAuth'

const Login = () => {

  const { hello } = useAuth() 

  console.log(hello);

...

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

AuthProvider:

const AuthContext = createContext()

const AuthProvider = ({children}) => {

    const hello= 'hello'

    return (
        <AuthContext.Provider value={{ hello }}>
          {children}
        </AuthContext.Provider>
      )
}

export {
     AuthProvider
}

export default AuthContext

UseAuth Hook:

import { useContext } from "react";
import AuthContext from "../context/AuthProvider";

const useAuth  = () => {

     return useContext(AuthContext)

}

export default useAuth

Context Routes

And this is the error:

Uncaught TypeError: Cannot read properties of undefined (reading 'hello')
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

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

<AuthProvider>
  <Login />
</AuthProvider>

Demo

Edit pedantic-stonebraker-myzk4e

References

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

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

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda