Introduction:
Before reading this article you should know about React Routing, its working.
Main Focus of this post is detecting user l...
For further actions, you may consider blocking this person and/or reporting abuse
Small bug:
Make sure to also set the lastLocation to null inside the method cancelNavigation.
Why?
Imagine:
Thanks a lot Muhammad! 🙏🏼
This was extremely helpful. Worked like a charm.
I had to modify useCallbackPrompt a little bit, as it didn't work properly when the component wasn't unmounted after a "confirmed navigation".
The changes are:
[confirmedNavigation, location]
- added location to the handleBlockedNavigation hook. The location object changes and thelocation.pathname
value remains outdated.setConfirmedNavigation(false);
- set after the programmatic navigation, state needed to be cleaned up so that future prompts are displayed.Sorry if I'm not clear enough. I'm short of time and cannot elaborate a lot. Hopefully it will be useful for someone.
Thanks.
Thank you Miguel Palacio for fixing :)
Using React Router Dom V6, there is no
navigator.block()
function. use custom history andhistory.block()
function instead, as mentioned in Drew Reese answer on stackoverflowThanks Muhammad for this post. I've been after a nifty solution for this.
One enhancement would be to check if the form value has actually changed, before triggering the dialog? This would mean comparing values in handleChange() and keeping the original form values/states. Any ideas how to implement this?
Hi thanks a lot.
When i click in page reload button, it shows the default browser alertbox. any idea how to solve it?
Having the same issue. Have you made any progress?
Could this be work with some updates if we have a multi-page form? e.g. different pathnames for each step "/form/step1" and "/form/step2"?
can u please give me a solution with react router dom version 6.8.1 ...this code is not working in my case becuase it gives an error that navigator.block is not a function ...can any one help me?
Facing same issue,Did you get any solution.?
I am getting the following error
Any idea how to solve it?
can we have custom popup for browser refresh as well. I tried your code & got the browser default only. Is there a way to show custom message on browser reload?
Could we have custom message in the browser prompt?
I think it's normal to not be able to control everything, it looks like we are not able to add custom functionality to onbeforeunload. developer.chrome.com/blog/chrome-5...
@jaisaro14 @jackwhisler1 @mouerr