svelte-routing icon indicating copy to clipboard operation
svelte-routing copied to clipboard

Trigger a function everytime a route changes

Open MarcGodard opened this issue 4 years ago • 2 comments

Hello, I am trying to figure out how to run an analytic function every time the route changes.

I did not find anything in the documentation for this. Is this not possible?

MarcGodard avatar Mar 01 '21 22:03 MarcGodard

Hey there,

Maybe issue https://github.com/EmilTholin/svelte-routing/issues/41#issuecomment-778765725 could be useful.

fcsbr avatar Mar 08 '21 02:03 fcsbr

I went a different direction:

  history.pushState = new Proxy(history.pushState, {
    apply (target, thisArg, argumentsList) {
     // run function stuff here
      Reflect.apply(target, thisArg, argumentsList)
    }
  })

This has been working very well.

MarcGodard avatar Mar 08 '21 11:03 MarcGodard

useLocation hook is a better options

krishnaTORQUE avatar Sep 08 '23 07:09 krishnaTORQUE

@krishnaTORQUE Do you have a code example with useLocation?I must have missed this in the docs?

MarcGodard avatar Sep 13 '23 16:09 MarcGodard

@krishnaTORQUE Do you have a code example with useLocation?I must have missed this in the docs?

https://github.com/EmilTholin/svelte-routing/issues/255#issuecomment-1687692609

krishnaTORQUE avatar Sep 13 '23 16:09 krishnaTORQUE