Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

264
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda