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

87
Vistas
React component's internal state is not updating when it is inside a context

I have a simple React component named Child which increments its count when clicked. Also, an outside state is saved which get updated when the Child component is incremented.

const Child = ({ onChange }) => {
  const [count, setCount] = useState(0);
  return (
    <div>
      <h1
        onClick={() => {
          setCount(prev => prev + 1);
          onChange(1);
        }}
      >
        Child count = {count}
      </h1>
    </div>
  );
};
const App = () => {
  const [count, setCount] = useState(0);

  return (
    <div className="App">
      <h1> Parent Count = {count} </h1>

      <Child onChange={(i) => setCount(count + i)} />
    </div>
  );
}

Above code works as expected. However, when I use the Context API to manage the outside state, the Child component's internal count remains at 0.

const App = () => {
  const [count, setCount] = useState(0);

  const AppContext = React.createContext();

  return (
    <div className="App">
      <AppContext.Provider value={{ state: count, setState: setCount }}>
        <>
          <AppContext.Consumer>
            {({ state }) => <h1> Parent Count = {state} </h1>}
          </AppContext.Consumer>

          <AppContext.Consumer>
            {({ state, setState }) => (
              <Child onChange={(i) => setState(state + i)} />
            )}
          </AppContext.Consumer>
        </>
      </AppContext.Provider>
    </div>
  );
};

Sample CodeSandbox.

Why the internal state of the element inside AppContext.Provider always reset to its initial state?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

First, the creation of a context should be outside of a Component, which means this line:

const AppContext = React.createContext()

should be outside of App. Second, a context setup can be simpler, as below (I separated each component in its own file, so it resembles to common use cases), and working forked CodeSandbox here.

App:

import { useState, createContext } from "react";
import Child from "./Child";
export const AppContext = createContext();

const App = () => {
  const [count, setCount] = useState(0);

  return (
    <div className="App">
      <h1> Parent Count = {count} </h1>
      <AppContext.Provider value={{ state: count, setState: setCount }}>
        <Child />
      </AppContext.Provider>
    </div>
  );
};

export default App;

Child:

import { useContext, useState } from "react";
import { AppContext } from "./App";

const Child = () => {
  const { state, setState } = useContext(AppContext);
  const [count, setCount] = useState(0);
  return (
    <div>
      <h1
        onClick={() => {
          setCount((prev) => prev + 1);
          setState(state + 1);
        }}
      >
        Child count = {count}
      </h1>
    </div>
  );
};

export default Child;
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