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

406
Vistas
React useContext() default value has not changed even I provide a value

Why is the UserContext default value has not changed even I specify a value in <UserContext.Provider>? How can I override the default value of the Context?

this is the App.jsx

import './App.css';
import React, { createContext } from 'react';
import ComponentB from './components/hooks/context/ComponentC';

function App() {
  return (
    <div className="App">
      <ComponentB />
    </div>
  );
}

export default App;

this is the ComponentB

import React, { createContext, useState } from 'react';
import ComponentC from './ComponentC';

export const UserContext = React.createContext('default');
export const ChannelContext = React.createContext('default');

const provider = UserContext.Provider;

function ComponentB() {
  return (
    <div>
      <provider value='Username'>
        <ComponentC />
      </provider>
    </div>
  );
}

export default ComponentB;

this is the ComponentC

import React from 'react';
import ComponentE from './ComponentE';

const ComponentC = () => {
  return <ComponentE />;
}

export default ComponentC;

this is the ComponentE

import React, { Component, useContext } from 'react';
import { UserContext, ChannelContext } from './ComponentB';

const ComponentE = () => {
  const username = useContext(UserContext);
  const channel = useContext(ChannelContext);

  return <div>username : {username} channel : {channel}</div>;
}

export default ComponentE;
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

In your App.jsx file, you say this:

import ComponentB from './components/hooks/context/ComponentC';
                ^                                           ^

Down the chain, this leads to this being rendered:

<div className="App">
  <div>
    username : {username} channel : {channel}
  </div>
</div>

As you can see, there's no provider.

Even still, if we fix this one character typo, the issue persists.

This is because you say

const provider = UserContext.Provider;

...
<provider>
  ...
</provider>

which isn't allowed.

If you do

<UserContext.Provider>
  ...
</UserContext.Provider>

it works.

https://codesandbox.io/s/wizardly-andras-csxxy?file=/src/App.js

Regarding the first issue, this is why you should do

export const MyComponent = () => <></>;
import { MyComponent } from "./MyComponent";

instead of

const MyComponent = () => <></>;
export MyComponent;
import MyComponent from "./MyComponent";
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