英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论