universal-router
universal-router copied to clipboard
How to implement onLeave logic with this router?
Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.
First of all you need to have a common onclick
handler for <a>
tags, then you may execute any code and after that prevent default behavior depending on the application state or continue.
function handleLinkClick(event) {
// "before leave" code here
if (condition) { // block?
event.preventDefault(); // block transition
} else {
// continue
}
}
Demo: https://jsfiddle.net/frenzzy/skgj8bd5/
If you are using History
module, you can use its API:
history.block((location, action) => {
// "before leave" code here
});
unblock();
Note that when user leaves the website completely you can use only window.onbeforeunload