![](/img/trans.png)
[英]React Warning: setState(…) Can only update a mounted or mounting component
[英]What does “Warning: setState(…): Can only update a mounted or mounting component” mean?
編輯
“警告:setState(...):只能更新已安裝或正在安裝的組件”是什么意思?
首先,將您所有的React組件重命名為Camel Case。
class firstChild ... --> class FristChild
<fristChild> --> <FristChild>
其次,在FirstChild
渲染方法中,應將元素包裝到一個封閉的標簽中,如下所示:
class FirstChild extends Component {
render(){
return (
<div>
<input ... />
<button ... />
</div>
)
}
}
第三,當你使用cloneElement
在this.props.children
,你應該使用Proptypes.<type>
在secondChildren
代替Propstypes.<type>.isRequired
。 在這里檢查,看看為什么。
class SecondChild extends Component {
static propTypes = {
submitSuccess: React.PropTypes.bool, // remove isRequired
}
}
不管上面什么,我已經測試了您的代碼,並且工作正常。
您可以嘗試使用componentWillUnmount
生命周期函數來檢查何時卸載了組件。
您還可以使用標志來指示在設置狀態之前已卸載組件:
saveName(nameText) {
if (!this.isUnmounted){
this.setState({submitSuccess: true});
}
}
componentWillUnmount() {
this.isUnmounted = true;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.