2017-03-16 47 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}/> 
    ) 
} 
比较