我有一个在react-router之外的组件,我想在路由更改时做一些事情。怎么做?
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;
答案 0 :(得分:0)
react-router's confirm navigation
也许这就是你所需要的,react-route的LifeCycle mixin给出了一个方法'routerWillLeave',它将在路线改变时被调用