Officially support & document router.events.
I am creating a component that needs visibility into router events. There are some awkward global functions you can set, like onRouteChangeComplete, but it's difficult to use this interface in the case where that property is already set. There's no documented way to attach to and detach from events. However, it appears that there's an EventEmitter under an events property that is available and would be perfect for this scenario:
import * as React from 'react';
class MyComponent extends React.Component {
// ...
componentDidMount() {
this.props.router.events.on('routeChangeComplete', this.handleRouteChange);
}
componentWillUnmount() {
this.props.router.events.off('routeChangeComplete', this.handleRouteChange);
}
}
Is this events interface officially supported? If so, should that be added to the documentation? My fear for using any undocumented feature is that it may go away at any time.
I briefly considered inspecting Router.onRouteChangeComplete functions to try to "add" a listener, but that is extremely hacky and doesn't lend itself well to multiple components subscribing/unsubscribing to events:
const originalFn = Router.onRouteChangeComplete;
Router.onRouteChangeComplete = url => {
originalFn(url);
// The rest of the functionality
};
// Then later
Router.onRouteChangeComplete = originalFn; // Not really safe
I'm also interested if this is the safe way to add multiple listeners to router events.
@timneutkens could you provide some info?
We ran into this with nextjs.org and hyper.is too. Especially when we start creating plugins to implement things like analytics etc. this will be a needed feature. This also solves unsubscribing to events, which doesn't seem reliably possible currently. Also, this doesn't even involve code changes, just documentation changes. So it seems like a great way to handle this.
Thanks for the great write-up @DullReferenceException ๐
Looks like we need slightly more than a documentation change since events isn't exposed via context.router. This seems to fix it: https://github.com/zeit/next.js/pull/4726
That's totally fine ๐
@DullReferenceException @timneutkens thanks for adding this! This is excellent. We were just working on analytics and ran into this so this is a welcome piece of work :) ๐ thanks guys :) :)
next-router-events was a workaround for this issue, see https://github.com/zeit/next.js/issues/2033#issuecomment-337461475. Glad I will soon be able to deprecate it.
๐
On Sep 4, 2018 4:50 AM, "Jayden Seric" notifications@github.com wrote:
next-router-events https://github.com/jaydenseric/next-router-events was
a workaround for this issue, see #2033 (comment)
https://github.com/zeit/next.js/issues/2033#issuecomment-337461475. Glad
I will soon be able to deprecate it.
โ
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/zeit/next.js/issues/4679#issuecomment-418338148, or mute
the thread
https://github.com/notifications/unsubscribe-auth/AmdhIpODC8zzPYRmExMxb4fZJXmTdi3rks5uXmj5gaJpZM4U6SlI
.
Most helpful comment
@DullReferenceException @timneutkens thanks for adding this! This is excellent. We were just working on analytics and ran into this so this is a welcome piece of work :) ๐ thanks guys :) :)