New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Route change event #964
Comments
Thanks for your help, though I knew of this solution. I really prefer to let my router decide what location needs authentication instead of the component. As I use the programmatic API, this solution suits my needs:
|
Hi, There is way to use "onEnter" with JSX? |
+1 looking for answers on previous question from @calbertts |
This example: https://github.com/rackt/react-router/blob/master/examples/auth-flow/app.js#L129 suggests to me that you can. I'm not having luck making it work though, so I have the same question as @calbertts. |
+1 I have the same question as @calbertts (I couldn't get the example at https://github.com/rackt/react-router/blob/master/examples/auth-flow/app.js#L129 to work) |
+1 same here |
I manage to make the auth example work by adding "super()," in the constructors. class App extends React.Component { |
What about this issue now? |
master (forthcoming 1.0 API) has hooks on routes, check out the auth example and the docs |
@ryanflorence Not sure you read the discussion above, this issue concerns the |
Same question as @mcobzarenco As the doc seems to be for 1.0 and a lot of us still uses 0.13.3, a lot of examples do not work. |
If you're using 0.13.x, you'll want to be sure to reference the 0.13.x docs. I think we've got a broken link from the 0.13.x branch README that unfortunately sends you to docs for 1.0.x. We'll fix that. In the meantime, you can find docs for 0.13 here: https://github.com/rackt/react-router/tree/0.13.x/doc |
For detecting route change within a component (e.g. if just the URL parameter changes) I've managed to get change detection working with a mixin: module.exports = {
componentWillMount: function() {
this.setState({
_routePath: this.getPath(),
});
},
componentWillUpdate: function() {
var newPath = this.getPath();
if (this.state._routePath !== newPath) {
this.setState({
_routePath: newPath,
});
if (this.onRouteChanged) {
this.onRouteChanged();
}
}
},
}; Then in your component simply add a |
@hiddentao Do you have any idea why this is being triggered twice every route change? Edit: You should use "componentWillReceiveProps" instead of "componentWillUpdate" |
@frg Thanks for that. If |
I do it like this, ataching event to Location:
using Router.HistoryLocation
|
Since a few people above have asked about onEnter in jsx and I didn't see an answer, this appears to work just fine to me:
|
@andrx your solution is great. Saved me a bunch of trouble. There was a problem with your code, though, so I made a few modifications and ended up with this: componentDidMount() {
this.context.router.listen(this.locationHasChanged)
}
componentWillUnmount() {
this.context.router.unregisterTransitionHook(this.locationHasChanged)
} Works beautifully. |
Yeah |
Is there a way to listen to all route changes? For some reason my windows |
@dominictobias You can call the same function for all routes and use a callback to distinguish each route if you need a custom behavior. |
Thanks for the fast reply @KeitIG, I'm not sure if this is supposed to be internal but I discovered a more convenient approach: browserHistory.listen(function(ev) {
console.log('listen', ev.pathname);
});
<Router history={browserHistory}>{routes}</Router> |
Looks better indeed :) |
Please open a question on Stack Overflow and we'll answer it there. |
@dominictobias Thanks for that, I went with your solution. With a large number of routes it is not practical to add the callback to each one and also the onEnter function could get quite busy if you want to use it for authentication, authorisation etc. |
Any reason why |
@dominictobias I've spent all my day trying to find a way do handle route changes. THANKS A LOT! |
I test and give you this: console.log('this.context.router is same as this.props.router', this.context.router === this.props.router); //true
console.log('this.context.router is same as browserHistory', this.context.router === browserHistory); //false
console.log('this.context.router.push is same as browserHistory.push', this.context.router.push === browserHistory.push); //true |
Tried to find a method for this as well and ended up here, but I just realized I already have it:
Maybe I'm interpreting it wrong, but this seems to work fine for me, ref: https://github.com/ReactTraining/react-router/blob/master/docs/API.md#onupdate |
try to implement HOC (Higher order component), you can take reference below code
|
I need to do something before the route change. Is there any before route change event? |
@vnsrahul1304, try |
Hey,
I need to add a route change event handler within my route definition.
Using statics on handlers is not a solution (avoid boilerplate), also using Router.Run to track route changes isn't my preferred solution (not modular). Is there anything available like this?
Thanks in advance!
The text was updated successfully, but these errors were encountered: