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

191
Views
Await react router history.goBack

I want to navigate backwards after clicking a submit button in a form for which I used history.goBack but in case there is no previous page/history, i want to navigate to a screen with thank you message using history.replace.

const handleSubmit = () => {
     history.goBack(); // will do nothing if there's no previous history
     // If no prev history
     history.replace({pathname: `${url}/submit`});
}

Now for the case where there is previous history, history.goBack would navigate to the previous page, but it being asynchronous and history.replace being synchronous, it navigates to the /submit page first and then history.goBack is executed. How do i await the execution of history.goBack?

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

0

You could try to check if the user was from another route:

By checking history action:

const handleSubmit = () => {
     if (history.action !== 'POP') {
        history.goBack();
     }
     history.replace({pathname: `${url}/submit`});
}

action === 'POP' means it's the initial load of your page, and it also works when the user was from an external URL

about 4 years ago · Juan Pablo Isaza Report

0

const handleSubmit = () => {
    if (history.length > 2) {
        // if history is not empty, go back:
        window.History.back();
    } else if (url) {
        // go to specified fallback url:
        window.History.replaceState(null, null, url);
    } else {
        // go home:
        window.History.replaceState(null, null, '/');
    }
}

try this one, this is from the answer in this post, How to check if the user can go back in browser history or not, but it's not always working.

IMO, your task can't be done, because there's no way to tell if there's previous history. Besides your previous history stored in the React has nothing to do with the Browser history, because a user could first visit google.com and then arrive to your site. So implementing this is not going to work at all. Not to mention React itself is a single URL application, this of course complicate things a bit.

about 4 years ago · Juan Pablo Isaza Report

0

history.goBack();

// Register a popstate listener that executes only once during the next history entry change
window.addEventListener('popstate', onPopStateCallback, {once: true});

Create a onPopStateCallback and you can do the replace within the callback

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!