react-router icon indicating copy to clipboard operation
react-router copied to clipboard

Allow observing of route transitions

Open lmb opened this issue 12 years ago • 11 comments

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.

lmb avatar Sep 19 '14 11:09 lmb

we used to have onActiveChange or something like that. I think we need to bring it back, I have the identical use case.

ryanflorence avatar Sep 22 '14 14:09 ryanflorence

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 ?

mjackson avatar Sep 22 '14 16:09 mjackson

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.

ryanflorence avatar Sep 22 '14 19:09 ryanflorence

Sounds good to me. Just for my understanding, this would declared on a Routes object?

lmb avatar Sep 24 '14 08:09 lmb

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

ryanflorence avatar Oct 04 '14 19:10 ryanflorence

JUST KIDDING

use <Routes onChange={fn} />, check the 0.7.0 -> 0.9.0 section in the upgrade guide.

ryanflorence avatar Oct 06 '14 20:10 ryanflorence

Thanks for the heads up, will do!

lmb avatar Oct 14 '14 08:10 lmb

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 avatar Dec 30 '14 20:12 mweibel

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

gaearon avatar Dec 30 '14 20:12 gaearon

Ah! :smile: Thanks for your fast answer. Didn't think about this one ;)

mweibel avatar Dec 30 '14 20:12 mweibel

Just to add, there's also an example of how to set up your own events triggered within the run callback.

agundermann avatar Dec 30 '14 20:12 agundermann