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

111
Visualizações
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 à 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