I have a component outside react-router, and I want to do something when route changes. How to do this?
class MyComponent extends React.Component {
constructor(props, context) {
super(props, context);
// get location and history here, but I don't know if these can help
this.context = context;
}
//TODO: do something when route changes
render() {
return (
<div>
my component
</div>
);
}
};
MyComponent.contextTypes = {
location: React.PropTypes.object,
history: React.PropTypes.object
};
module.exports = MyComponent;
react-router's confirm navigation
Maybe that's what you need,react-route's LifeCycle mixin give a method 'routerWillLeave' which will be called when the route will change
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.