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

110
Vistas
Providing values for react context twice on createContext and Provider component

React context usage with createContext does have an optional default value and most of the explanations on web are something like below.

import { createContext } from 'react';
const Context = createContext('Default Value');

then we have the Provider components on the context object which makes the child components capable to listen the context using Consumer components.

function Main() {
  const value = 'My Context Value';
  return (
    <Context.Provider value={value}>     /* Why is it mandatory to provide value attribute when we have the default value argument in createContext method. */
      <MyComponent />
    </Context.Provider>
  );
}

and then Using Consumers we can get the values without worrying about using the props way of passing data in React js.

<Context.Consumer>
      {value => <span>{value}</span>}   //context value in anonymous inner function
    </Context.Consumer>

Why do we need to separate values for context using the createContext method and then on Provider component ? can't we use the value passed to createContext method itself ? Thanks in advance.

about 4 years ago · Juan Pablo Isaza
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