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

263
Visualizações
React useContext() not reading context values in?

This is what my code looks like:

Context.js:

const Context = createContext();
export default Context;

ContextProvider.js:

import Context from './Context';
const ContextProvider = () =>
{
   ....
   return(<Context.Provider value={data: 1}>{props.children}</Context.Provider>
}

ParentClass.js:

const ParentClass = () => 
{
   ...
   return(
      <div>
       ...
      {boolValue ? (
      <ContextProvider>
         <ConsumerComponent/>
      </ContextProvider>) 
      </div>)
}

ConsumerComponent.js:

import Context from './Context.js';
const ConsumerComponent = () => {
   const contextData = useContext(Context);
   ...
}

My issue is that ConsumerComponent doesn't seem to be able to access context data; it doesn't render at all when I add the const contextData = useContext(Context); line, and nothing gets logged when I try to print contextData. Where exactly did I go wrong? From my understanding, I followed the necessary steps of creating context + a provider, making sure that the consuming component has a provider as one of its ancestor components, and then accessing the context with useContext().

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Considering you want the value prop of the Context Provider to be an object like

{ data : 1 }

you probably forgot the extra curly braces, because the first pair is the JSX syntax to interpret the content as JavaScript instead of a string.

So your value prop on ContextProvider.js file probably should look like this:

<Context.Provider value={{data: 1}}>{props.children}</Context.Provider>
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