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 add a confirm prompt before leave? #250

Closed
lxuanchengdx opened this issue Jun 26, 2020 · 1 comment
Closed

How to add a confirm prompt before leave? #250

lxuanchengdx opened this issue Jun 26, 2020 · 1 comment

Comments

@lxuanchengdx
Copy link

We're having to add confirmation before leaving, I see that Navigo has the before, but it's not receiving event.

Is there an alternative solution for this?

krasimir pushed a commit that referenced this issue Dec 29, 2020
krasimir pushed a commit that referenced this issue Dec 29, 2020
@krasimir
Copy link
Owner

I'll suggest to use the newest 8.0.0 beta version. There is a leave hook that accepts a callback.

router
  .on(
    "/about",
    (match) => {
      render("About");
    },
    {
      leave: (done) => {
        if (confirm("You are about to leave the About page. Are you sure?")) {
          done();
        } else {
          done(false);
        }
      },
    }
  )

When the user lands on /about and tries to go out will see that confirmation window. The router does nothing until you call the done function. (the full example here)

P.S.
Version 8.0.0 is not officially released but can be installed via npm install navigo@beta or yarn add navigo@beta. Migration guide is available here https://github.com/krasimir/navigo/blob/big-rewrite/CHANGELOG.md#migration-guide and the new documentation here https://github.com/krasimir/navigo/blob/big-rewrite/DOCUMENTATION.md.

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