2017-02-08 36 views
1

下面是我的示例代碼當this.props.children改變

var Test = React.createClass({ 

componentDidMount: function(){ 
    //do something 
} 

render: function() { 
    return (
     <div id="testDiv"> 
      <Header 
       messages={this.props.mesage} /> 
      {this.props.children} 
      <Footer /> 
     </div> 
    ); 
} 
}); 

我會做一些componentDidMount如何重新呈現組件。 當this.props.children,componentDidMount不能運行時,爲什麼?

當this.props.children每次都改變時,如何運行函數?

回答

2

componentDidMount僅在安裝組件時運行一次。您應該使用componentWillUpdate,在接收新道具或狀態時立即調用,或者在安裝組件收到新道具之前調用componentWillReceiveProps

+0

真的很感激。我從不使用componentWillUpdate,所以我完全忘記了它。 – Dreams