ComponentWillReceiveProps没有被调用

时间:2018-11-15 05:31:48

标签: reactjs

代码如下。

state = {
  showSection: false,
  selectedAccount: ''
};

render方法的定义如下。

return (
      <div>
        <button onClick={this.selectAccount} className="btn btn-default">Select Account</button> 
        {
           this.state.showSection &&
               <div>
                 <AccountSelectionForm handleAccordionClick= 
                      {this.handleAccordionClick}
                      selectedAccount={this.state.selectedAccount}
                 />  
               </div>
        }
      </div>
      )

selectAccount方法的定义如下。

  selectAccount = () => {
     this.setState({selectedAccount: 'A123'})
  }
  

AccountSelectionFormComponent

componentWillReceiveProps(nextProps) {
    if(nextProps.selectedAccount) {
      this.setState({
        accountId: nextProps.selectedAccount
      });
    }
  }

单击按钮后,selectedAccount值将更改。道具值也被更改,因为它是在道具中传递的。

在AccountSelectionForm组件中,我编写了componentWillReceiveProps方法以将selectedAccount设置为状态。

我的假设是,因为更改了prop,所以它将调用componentWillReceiveProps方法。 问题在于没有调用componentWillReceiveProps方法。

问题:

是由于AccountSelectionForm仍未在DOM中呈现此问题(this.state.showSection仍为false)。

关于如何解决此问题的任何想法?

0 个答案:

没有答案
相关问题