2017-03-16 45 views
2

我有一個是這樣寫的無狀態組件比較PARAM每次家長呈現

//stateless component 
const ComponentOne = ({ month }) => { 
    console.log(month); // print incremental of 1 when I render parent component 
} 

,月從其父母傳給一個無狀態的組件。

render(){ 
    return(
     <ComponentOne month={ month }/> 
    ) 
} 

它它的父母我可以改變月份並將其傳遞下去。如何在每次父母呈現後檢查月份是否有所不同?使用componentWillRecieveProps?但是這個組件是一個無國籍的組件。

回答

0

愚蠢的組件必須用於渲染的目的,你應該處理你主要組件中的所有邏輯。

您可以驗證該月的父組件的功能中,也可以通過其他道具啞組件,這將是本月支柱的前值,然後它像

const ComponentOne = ({ month, prevMonth }) => { 

    if(month != prevMonth) { 
      //do what you want here 
    } 
    console.log(month); // print incremental of 1 when I render parent component 
} 

render(){ 
    return(
     <ComponentOne month={ month } prevMonth={this.state.prevMonth}/> 
    ) 
} 
比較