例如:

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
    })
  }
}

.

 

相关文章:

  • 2021-06-05
  • 2021-12-12
  • 2021-04-14
猜你喜欢
  • 2021-10-10
  • 2021-12-21
  • 2022-01-16
  • 2021-10-17
  • 2022-12-23
  • 2021-04-13
相关资源
相似解决方案