preactjs / preact-router

:earth_americas: URL router for Preact.
http://npm.im/preact-router
MIT License
1.01k stars 156 forks source link

onChange event triggers only after URL change, unable to abort route transition before URL change #428

Open Aadhisivam opened 2 years ago

Aadhisivam commented 2 years ago

I would like to abort route transition based on certain conditions , so i utilised onChange event , but it triggers only after URL change. I can abort transition in that event, but the url hash is changed which is irrelevant of the current route.

Is there any way to block this behaviour ?

Aadhisivam commented 2 years ago

Edit : Cant Abort onChange event also

tanjim-rahat commented 2 years ago

Adding a setTimeout worked for me

const onRoute = async ev => {
      switch (ev.path) {
          case '/login': {
              if (auth == true) setTimeout(() => route('/home', true), 0)
          }
      }
  }
akornatskyy commented 1 year ago

the route cannot be used as the active router is registered in ROUTERS when it mounted (that means at the time you call route, the ROUTES is empty list); so instead you can use ev.router.routeTo(...).