DEV Community

Discussion on: Detecting user leaving page with react-router-dom v6.0.2

Collapse
 
mleister97 profile image
mleister97

Small bug:

Make sure to also set the lastLocation to null inside the method cancelNavigation.

Why?

Imagine:

  1. Filling out a form
  2. Switch page
  3. "Are you sure dialog" will open
  4. Cancel the dialog (stay on page)
  5. Update the form values again
  6. Submit the form via the default submit button
  7. Hook will keep the last location reminded and switch page now, even if not intended