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

187
Views
How to use browser history with components?

Suppose I have following code:

const [showChild1, setShowChild1] = useState(false)
const [showChild2, setShowChild2] = useState(false)

        <ParentComponent>
          {showChild1 && <Child1 />}
          {showChild1 && <Child2 />}
           ....
      </ParentComponent

Suppose I am viewing Child2 in my browser, but I want to go back to see Child1. How can I use the browser's back and forward buttons to navigate between conditionally rendered components? As of now when i click on the back button im pushed to path /.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can add event listeners to the global window object. The event name is "popstate", here is the code example:

  useEffect(() => {
    window.history.pushState(null, null, window.location.pathname);
    window.addEventListener('popstate', yourEvent);
    return () => {
      window.removeEventListener('popstate', yourEvent);  
    };
  }, []);
about 4 years ago · Juan Pablo Isaza Report

0

use onpopstate event to handle browser navigation options.

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!