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

138
Views
How to maintain component state after page rerender/change?

I have a counter made using context API and I need it to maintain the current state after the page is changed. Is there any way to keep the Counter value on page change like in session storage or something? I'm using react router to change the pages.

export const CounterContext = React.createContext()



function App() {

  const [Counter, setCounter] = useState(0)
  useEffect(() => {
    setCounter(JSON.parse(window.localStorage.getItem('count')));
  }, []);

  useEffect(() => {
    window.localStorage.setItem('count', Counter);
  }, [Counter]);
  
  function ChangeCounter() {
    setCounter(Counter+1);
  }
  console.log(Counter);

  return (
    <CounterContext.Provider value={{Counter, ChangeCounter}}>
    <Router>
    <div className="App">
      <header className="App-header">
        <Navbar />
      </header>
      <div className="Content">
        <Switch>
          <Route exact path="/">
            <Home />
          </Route>
          <Route path="/Products">
            <Products />
          </Route>
          <Route path="/Contact">
            <Contact />
          </Route>        
        
        </Switch>
      </div>
      
      <footer>
        <Footer />
      </footer>
    </div>
    </Router>
    </CounterContext.Provider>
  );
}

Edit:
I made it using local storage but I'm not really sure is that what I wanted. Now even on browser restart the counter stay on the same updated value but I want it to refresh. Is there a way to do that now?
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Ok I made it. Thanks a lot everyone! I used the sessionStorage to stor the data only during the session. It looks like this:

export const CounterContext = React.createContext()



function App() {

  const [Counter, setCounter] = useState(0)
  useEffect(() => {
    setCounter(JSON.parse(window.sessionStorage.getItem('count')));
  }, []);

  useEffect(() => {
    window.sessionStorage.setItem('count', Counter);
  }, [Counter]);

  function ChangeCounter() {
    setCounter(Counter+1);
  }
  console.log(Counter);
//and the rest  of the code...
}
about 4 years ago · Juan Pablo Isaza Report

0

You can use cookies for this functionality. You can prefer universal-cookie to manipulate cookies in react

npm install universal-cookie
about 4 years ago · Juan Pablo Isaza 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!