preactjs / preact-router

:earth_americas: URL router for Preact.

Home Page:http://npm.im/preact-router

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

Aadhisivam opened this issue · comments

commented

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 ?

commented

Edit : Cant Abort onChange event also

Adding a setTimeout worked for me

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

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(...).