1
我在react-router外有一個組件,我想在路由更改時做一些事情。這個怎麼做?當路由發生變化時,如何處理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;