Javascript 当路由器外的路由发生变化时,如何处理?

Javascript 当路由器外的路由发生变化时,如何处理?,javascript,react-router,Javascript,React Router,我在react路由器外面有一个组件,我想在路由改变时做些什么。如何做到这一点 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; }

我在react路由器外面有一个组件,我想在路由改变时做些什么。如何做到这一点

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;
类MyComponent扩展了React.Component{ 构造函数(道具、上下文){ 超级(道具、背景); //在这里获取位置和历史,但我不知道这些是否有帮助 this.context=上下文; } //TODO:当路线改变时,做些什么 render(){ 返回( 我的组件 ); } }; MyComponent.contextTypes={ 位置:React.PropTypes.object, 历史记录:React.PropTypes.object }; module.exports=MyComponent;
也许这正是您所需要的,react route的生命周期mixin提供了一个方法“routerWillLeave”,当路由更改时将调用该方法 也许这正是您所需要的,react route的生命周期mixin提供了一个方法“routerWillLeave”,该方法将在路由更改时调用