question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

<Prompt /> component from react-router-dom misbehaves on cancelling navigation

See original GitHub issue

I’ve recently migrated from version 3 to 4.1.2 in the hope that the following bug caused by the setRouteLeaveHook won’t be present in the <Prompt /> component from react-router-dom.

So, as per the use case of Prompt component, when the user standing in the form refreshes the page, edit some fields and clicks back button, the confirmation alert pops up and when the user cancels the navigation, it stays on the page but the url gets changed and does not remain the same as of the current form page.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:80
  • Comments:62 (14 by maintainers)

github_iconTop GitHub Comments

27reactions
arvinsinglacommented, Aug 3, 2017

I’m experiencing the exact same thing using BrowserRouter. If the user clicks a link or does an operation that pushes a route to history everything works as expected. If however the user clicks the browser back button I get a prompt as expected but the URL in the address bar has changed to the previous path.

If I then click cancel on the prompt indicating I do not wish to leave, one of two things will happen.

  1. The current route is restored in the address bar
  2. The previous route stays in the address bar

In both cases my current component is still shown and my components lifecycle hooks includingcomponentWillReceiveProps are called which messes up the state of my component.

25reactions
matthieudesprezcommented, Mar 19, 2018

I experienced the same issue.

When I come from an in app route, the behaviour of Prompt is working as expected, but when I access the page directly with the url of the view containing the Prompt component and I trigger it, the browser url remains modified even if I click on cancel.

I made a repo to reproduce the issue : https://github.com/Edistra/react-router-dom-prompt-issue

I also made a gif of the issue :

ezgif com-video-to-gif

Read more comments on GitHub >

github_iconTop Results From Across the Web

<Prompt /> component from react-router-dom misbehaves on ...
The issue is that the prompt is allowing the URL to change even after clicking on cancel navigation. This happens only after you...
Read more >
Prevent Navigation with the React Router Prompt Component ...
In this lesson we'll show how to setup the Prompt component from React Router. ... return `true` from the message as a function...
Read more >
React-router Prompt: How do I actually override the cancel ...
I looked into using react-router Prompt component and I have it ... the browser confirmation dialog and not navigate away from the page....
Read more >
Prevent Navigation with the React Router Prompt Component
In this lesson we'll show how to setup the Prompt component from React Router. We'll prompt with a static message, as well as...
Read more >
Error When Back Button Is Clicked When Using React Router ...
Ask questions<Prompt /> component from reactrouterdom misbehaves on cancelling navigation. I've recently migrated from version 3 to 4.1.2 in the hope that.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found