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.

Pre-conditions (route guards) to handle close modal

See original GitHub issue

Hello, svelte-spa-router is really fine and works as expected. Thank you.

But I need to handle the browser history back to close a modal. So i have read the doc at https://github.com/ItalyPaleAle/svelte-spa-router/blob/master/Advanced Usage.md#route-pre-conditions

I have a store variable which tells me if I accept to route or not.

If not, I expected “svelte-spa-router” to do nothing. Sure it does not load any route but it seems to unload the current component.

With Vue I dit it like this :

router.beforeEach((to, from, next) => {
  if (store.state.isOpenModal) {
    store.state.isOpenModal = false // close modal
    next(false) // cancel routing
    return
  }
  next() // accept routing
})

This works perfectly even if I don’t understand how they prevent onpopstate(). I transposed like this in Svelte :

const routes = {
  '/': wrap(
    Home,
    () => { return nextOrNot() }
  ),
  '/about': wrap(
    About,
    () => { return nextOrNot() }
  )
}

function nextOrNot () {
  if (isOpenModal) {
    iisOpenModal.set(false)
    return false
  }
  return true
}

My modal is closed but my current component is gone.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

5reactions
ItalyPaleAlecommented, May 7, 2020

I’ll take a look at this and see what can be done

0reactions
ItalyPaleAlecommented, Dec 23, 2020

I’m not sure how I would do that, I’d have to experiment.

One lead (which might or might not work) could be using a route guard to block the new component from mounting if the form is dirty, and inside the route guard invoke push() to send the user back to the page they were on before. This would likely still cause the component to be un-mounted, so you’d have to restore the state of the form manually.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Routing to Angular Material Dialogs | by John Crowson | ngconf
open/close handled in DialogEntryComponent }. Declare the dummy component and configure the Router Module to load it on 'dialog' navigation ...
Read more >
Router tutorial: tour of heroes - Angular
In this tutorial, you build upon a basic router configuration to explore features such as child routes, route parameters, lazy load NgModules, guard...
Read more >
How to deal with a modal shown in a guard - Stack Overflow
When I type an unauthorized route in my browser's address bar, the server-side rendering shows an inert modal, then when the client-side takes ......
Read more >
Angular Basics: CanActivate—Introduction to Routing Guards
We'll look at ensuring the routes we create in Angular are accessed by the right people and preventing unauthorized access to routes that ......
Read more >
How to Create a Modal Route with React Router - Morioh
modal -wrapper , which would close the modal when the actual .modal element is activated. import React from 'react'; import { withRouter }...
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