Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to implement onLeave logic with this router? #100

Open
Stupidism opened this issue Jun 7, 2017 · 1 comment
Open

How to implement onLeave logic with this router? #100

Stupidism opened this issue Jun 7, 2017 · 1 comment

Comments

@Stupidism
Copy link

Stupidism commented Jun 7, 2017


Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

@frenzzy
Copy link
Member

frenzzy commented Jun 8, 2017

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants