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

200
Vistas
Context values not updating in react js

I have created a counter app in React js using context api for global state management .

But the problem is when i am clicking increase and decrease button it is not updating global values .

I am new to react , please provide guidance what is going wrong here .

ContextFile :

import {createContext,useState} from 'react';

export const DataContext = createContext({
    data:0,
    increase : () => {},
    decrease : () => {}
});

function DataContextProvider(props){
    const [data,setData] = useState();

    const increase = () => {
        setData(data + 1);
    }

    const decrease = () => {
        setData(data - 1);
    }

    return(
        <DataContext.Provider value={{data,increase,decrease}}>
            {props.children}
        </DataContext.Provider>
    );

};

export default DataContextProvider;

App.js :

import React,{useContext} from 'react';
import {DataContext} from './Context/dataContext';
import DataContextProvider from './Context/dataContext';
import IncreaseBtn from './Component/Increase';
import DecreaseBtn from './Component/Decrease';

const App = () => {
  const {data} = useContext(DataContext);
  
  return(
    <>
    <DataContextProvider>
      {data}
      <br/>
      <br/>
      <IncreaseBtn />
      <br/>
      <br/>
      <DecreaseBtn />
    </DataContextProvider>
    </>
  )
}

export default App;

Increase Button Component :

import React,{useContext} from 'react';
import {DataContext} from '../Context/dataContext';

const IncreaseBtn = () => {
    const {increase} = useContext(DataContext);

    return(
        <>
            <button onClick={increase}> Increase </button>
        </>
    )
}

export default IncreaseBtn;

Decrease Button Component :

import React,{useContext} from 'react';
import {DataContext} from '../Context/dataContext';

const DecreaseBtn = () => {
    const {decrease} = useContext(DataContext);

    return(
        <>
            <button onClick={decrease}> Decrease </button>
        </>
    )
}

export default DecreaseBtn;

Folder Structure : enter image description here

almost 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

  • If you want to use context you should wrap your provider around those components, but here App component isn't wrapped but to its children 😉
  • Give an initial state of some "number" as it would be undefined and it gives NaN if you do the arithmetic operations with it.

Updated the sandbox for your ref

almost 4 years ago · Santiago Trujillo Denunciar

0

You are updating the state in the wrong way Try: setCount(count => count + 1);

almost 4 years ago · Santiago Trujillo 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