Allow observing of route transitions
Would it be possible to expose an interface to observe all route changes? Can be completely read-only, I'd like to fire off Google Analytics events every time someone navigates on the single page app I am building.
we used to have onActiveChange or something like that. I think we need to bring it back, I have the identical use case.
Yeah, the problem with the previous impl of onActiveStateChange is that it had side effects that people weren't aware of.
Let's put it back in without the side effects and call it something different. onPathChange ?
onRoute
onRouteChange
onPathChange
onTransition
I'm fine with any of those.
I need this for yet another use case: keeping URLs in sync with iframe'd sub applications via window.postMessage :)
We might even consider:
onTransitionToRoute
onTransitionFromRoute
And give devs an opportunity to abort globally based on who-knows-what.
Sounds good to me. Just for my understanding, this would declared on a Routes object?
We have a solution in v0.8.0. See the upgrade guide:
https://github.com/rackt/react-router/blob/master/UPGRADE_GUIDE.md#routes-onactivestatechange---pathstate
JUST KIDDING
use <Routes onChange={fn} />, check the 0.7.0 -> 0.9.0 section in the upgrade guide.
Thanks for the heads up, will do!
Since there is no Routes anymore, what do you recommend to do instead?
I noticed I can add a change listener to the Location object of choice (e.g. Router.HistoryLocation.addChangeListener(onRouteChange);) but I think there should be a better option :)
@mweibel
As of 0.11, you pass a callback when initializing the router. That callback is your change handler. (Normally you render in it, but you can do anything else as well.)
Ah! :smile: Thanks for your fast answer. Didn't think about this one ;)
Just to add, there's also an example of how to set up your own events triggered within the run callback.