2015-12-11 58 views
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; 

回答

0

react-router's confirm navigation
也許這就是你所需要的,反應路線的生命週期混入給出一個方法「routerWillLeave」將被調用時的路線會改變

相關問題