[英]What does “Warning: setState(…): Can only update a mounted or mounting component” mean?
EDIT 编辑
What does "Warning: setState(...): Can only update a mounted or mounting component" mean? “警告:setState(...):只能更新已安装或正在安装的组件”是什么意思?
Fisrt, rename all your React components as Camel Case like this. 首先,将您所有的React组件重命名为Camel Case。
class firstChild ... --> class FristChild
<fristChild> --> <FristChild>
Second, in your FirstChild
render method, you should wrap your elements into an enclosing tag like this: 其次,在
FirstChild
渲染方法中,应将元素包装到一个封闭的标签中,如下所示:
class FirstChild extends Component {
render(){
return (
<div>
<input ... />
<button ... />
</div>
)
}
}
Third, when you use cloneElement
upon this.props.children
, you should use Proptypes.<type>
in your secondChildren
instead of Propstypes.<type>.isRequired
. 第三,当你使用
cloneElement
在this.props.children
,你应该使用Proptypes.<type>
在secondChildren
代替Propstypes.<type>.isRequired
。 Check it here to see why. 在这里检查,看看为什么。
class SecondChild extends Component {
static propTypes = {
submitSuccess: React.PropTypes.bool, // remove isRequired
}
}
Regardless all above, I have tested your code and it works fine. 不管上面什么,我已经测试了您的代码,并且工作正常。
You can try and use componentWillUnmount
lifecycle function in order to check when the component is unmounted. 您可以尝试使用
componentWillUnmount
生命周期函数来检查何时卸载了组件。
You can also use a flag to signal that the component is unmounted before setting the state: 您还可以使用标志来指示在设置状态之前已卸载组件:
saveName(nameText) {
if (!this.isUnmounted){
this.setState({submitSuccess: true});
}
}
componentWillUnmount() {
this.isUnmounted = true;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.