"Warning: setState(...): Can only update a mounted or mounting component" 是什么意思?
What does "Warning: setState(...): Can only update a mounted or mounting component" mean?
编辑
"Warning: setState(...): Can only update a mounted or mounting component" 是什么意思?
您可以尝试使用 componentWillUnmount
生命周期函数来检查组件何时卸载。
您还可以在设置状态之前使用标志来指示组件已卸载:
saveName(nameText) {
if (!this.isUnmounted){
this.setState({submitSuccess: true});
}
}
componentWillUnmount() {
this.isUnmounted = true;
}
首先,像这样将所有 React 组件重命名为 Camel Case。
class firstChild ... --> class FristChild
<fristChild> --> <FristChild>
其次,在您的 FirstChild
渲染方法中,您应该将元素包装到一个封闭的标记中,如下所示:
class FirstChild extends Component {
render(){
return (
<div>
<input ... />
<button ... />
</div>
)
}
}
第三,当你在 this.props.children
上使用 cloneElement
时,你应该在 secondChildren
中使用 Proptypes.<type>
而不是 Propstypes.<type>.isRequired
。检查它 here 看看为什么。
class SecondChild extends Component {
static propTypes = {
submitSuccess: React.PropTypes.bool, // remove isRequired
}
}
尽管如此,我已经测试了你的代码并且它工作正常。
编辑
"Warning: setState(...): Can only update a mounted or mounting component" 是什么意思?
您可以尝试使用 componentWillUnmount
生命周期函数来检查组件何时卸载。
您还可以在设置状态之前使用标志来指示组件已卸载:
saveName(nameText) {
if (!this.isUnmounted){
this.setState({submitSuccess: true});
}
}
componentWillUnmount() {
this.isUnmounted = true;
}
首先,像这样将所有 React 组件重命名为 Camel Case。
class firstChild ... --> class FristChild
<fristChild> --> <FristChild>
其次,在您的 FirstChild
渲染方法中,您应该将元素包装到一个封闭的标记中,如下所示:
class FirstChild extends Component {
render(){
return (
<div>
<input ... />
<button ... />
</div>
)
}
}
第三,当你在 this.props.children
上使用 cloneElement
时,你应该在 secondChildren
中使用 Proptypes.<type>
而不是 Propstypes.<type>.isRequired
。检查它 here 看看为什么。
class SecondChild extends Component {
static propTypes = {
submitSuccess: React.PropTypes.bool, // remove isRequired
}
}
尽管如此,我已经测试了你的代码并且它工作正常。