Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

198
Views
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 answers
Answer question

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 Report

0

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

almost 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!