【发布时间】:2020-07-01 15:57:26
【问题描述】:
我正在做一个项目,需要在 componentDidMount 之后设置状态。(期望子组件中的道具是在挂载时派生的。因此我只能在之后设置状态) 我能想到的唯一选择是使用 componentDidUpdate。
props 父组件是一个从 axios 获取的数据派生的数组。
这里的目标是遍历数组并从下面代码中显示的 URL 中为每个数组获取数据,然后设置子组件的 setState。
尝试我通常做的事情,我无法停止在 componentDidUpdate 处触发的无限循环。
这是我的代码。
父母
render(){
return (
<div className="App">
<EachCountry countryList= {this.state.CountriesList}/>
</div>
子组件
async componentDidUpdate(prevProps, prevState, snapshot){
if(this.state.countryList.length < this.props.countryList.length){
await this.props.countryList.map(m=>{
axios ({
method:"get",
url:`/countryupdate/${m}`
}).then(res=>{
console.log(res.data)
this.setState(crntState=>({
countryList:[...crntState.countryList, res.data]
}))
})
})
}
}
控制台日志运行良好。但是当我尝试 setState 时,我遇到了像 5000 多条错误消息这样的无限循环。
我的另一个技巧是
async componentDidUpdate(prevProps, prevState, snapshot){
if(this.state.countryList.length < this.props.countryList.length){
await this.props.countryList.map(m=>{
var newdata = axios ({
method:"get",
url:`/countryupdate/${m}`
})
console.log(newdata)
this.setState(crntState=>({
countryList:[...crntState.countryList, newdata.data]
}))
})
}
}
而这个返回的是承诺而不是所需的数据。
帮助家庭
我错过了什么?
【问题讨论】:
-
ComponentDidUpdate 中的 setState 将创建一个无限循环,因为 setState 将更新组件,并且在更新时将调用 ComponentDidUpdate 生命周期方法
-
@GowriPranithBayyana 并根据文档,为避免这种情况,开发人员应将条件语句放在实际 setState 的头部。我做到了。你猜怎么着,我仍然遇到这个循环。你建议我应该怎么做。
标签: javascript reactjs setstate