如何远离 componentWillReceiveProps

huangapple go评论63阅读模式
英文:

How to move away from componentWillReceiveProps

问题

由于 componentWillReceiveProps 已被弃用,我们应该如何迁移以下代码?

componentWillReceiveProps (nextProps) {
    if (nextProps.reloadData && nextProps.realoadDataId) {
        this.props.onChange({target: {id: 'reloadData', value: false}})
        this.props.reloadData();
    }

    if (this.props.dataUpdated) {
        this.setState({
            sucessMessage: 'Updated Successfully'
        })
    } 
    if (nextProps.error) {
        this.props.setToast(nextProps.error, true)
    }
    if (nextProps.downloadDataList && nextProps.downloadDataList.length > 0) {
        downloadDataAsExcel(nextProps.downloadDataList);
        this.props.onChange({target: {id: 'downloadDataList', value: null}})
    }
}

提前感谢。

英文:

As componentWillReceiveProps is deprecated, How do we migrate the below code?

componentWillReceiveProps (nextProps) {
		if (nextProps.reloadData && nextProps.realoadDataId) {
            this.props.onChange({target: {id: 'reloadData', value: false}})
			this.props.reloadData();
		}

		if (this.props.dataUpdated) {
			this.setState({
				sucessMessage: 'Updated Successfully'
			})
		} 
        if (nextProps.error) {
			this.props.setToast(nextProps.error, true)
		}
        if (nextProps.downloadDataList && nextProps.downloadDataList.length > 0) {
			downloadDataAsExcel(nextProps.downloadDataList);
			this.props.onChange({target: {id: 'downloadDataList', value: null}})
		}
}

Thanks in advance

答案1

得分: 1

你可以使用static getDerivedStateFromProps(nextProps, prevState)方法来复制上述行为。

static getDerivedStateFromProps(nextProps, prevState) {
    if (nextProps.reloadData && nextProps.realoadDataId) {
        nextProps.props.onChange({ target: { id: 'reloadData', value: false } })
        nextProps.props.reloadData();
    }

    if (nextProps.error) {
        nextProps.setToast(nextProps.error, true)
    }

    if (nextProps.downloadDataList && nextProps.downloadDataList.length > 0) {
        downloadDataAsExcel(nextProps.downloadDataList);
        nextProps.onChange({ target: { id: 'downloadDataList', value: null } })
    }

    if (nextProps.dataUpdated) {
        return {
            successMessage: 'Updated Successfully'
        }
    }

    return null;
}

你可以使用return语句基于传入的props来获取状态。

英文:

You can use static getDerivedStateFromProps(nextProps, prevState) method to replicate the above behaviour.

    static getDerivedStateFromProps(nextProps, prevState) {
            if (nextProps.reloadData && nextProps.realoadDataId) {
                nextProps.props.onChange({target: {id: 'reloadData', value: false}})
                nextProps.props.reloadData();
            }
    
            if (nextProps.error) {
                nextProps.setToast(nextProps.error, true)
            }
            
if (nextProps.downloadDataList && nextProps.downloadDataList.length > 0
         {
             downloadDataAsExcel(nextProps.downloadDataList);
                nextProps.onChange({target: {id: 'downloadDataList', value: null}})
    
      }
if (nextProps.dataUpdated) {
                return {
                     sucessMessage: 'Updated Successfully'
                 }
            } 
return null;

}

You can use the return statement to get the state based on the incoming props.

huangapple
  • 本文由 发表于 2023年6月12日 22:41:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76457750.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定