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

141
Views
Prompt users of unsaved changes if they try navigate to another page in ReactJS

I am working on a dynamic form where the form gets submitted automatically when all the required fields are filled. We don't have any submit button in the form. Now suppose a user has filled a few of the form fields and tries to leave the form by clicking any navigation link in the application, we need to warn the user with a pop up showing the message "You have unsaved changes. Navigating away from this view will revert these changes. This action cannot be undone. Are you sure you want to continue?". Now if the user clicks on the continue it will redirect to the navigated page and if he clicks on cancle it will stay on the same page(form).

Here we are using ReactJS but not using any react routing in the application. We have tried all possible ways but were not able to come up with any solution. Has somebody faced this scenario before or has any idea how to approach the problem?

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

0

You could use native browser API beforeUnload.

  ...
  const beforeunload = (e) => {
    // You can add logic for confirmation here.
    if (dataUnsaved) {
      e.preventDefault();
      e.returnValue = true;
    }
  }

  useEffect(() => {
    window.addEventListener('beforeunload', beforeunload);
    
    return () => {
        window.removeEventListener('beforeunload', beforeunload);
    }

  }, [])

  return (
   ...
  )
...
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!