react 生命周期 componentWillReceiveProps(nextProps) 的替代方法


例如:

componentWillReceiveProps(nextProps) {
  if (this.props.editInfo.id !== nextProps.editInfo.id) {
    // 請求詳情數據
    this.props.getDetail({
      id: nextProps.editInfo.id
    })
  }
}

可寫成:

static getDerivedStateFromProps(props, state) {
  if (props.editInfo.id !== state.editInfo.id) {
    return {
      editInfo: props.editInfo
    };
  }
 
  return null;
}
 
componentDidUpdate(prevProps, prevState) {
  if (this.state.editInfo.id !== prevState.editInfo.id) {
    // 請求詳情數據
    this.props.getDetail({
      id: this.state.editInfo.id
    })
  }
}

.

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM