【发布时间】:2017-01-19 08:07:58
【问题描述】:
编辑
“Warning: setState(...): Can only update amounted ormounting component”是什么意思?
【问题讨论】:
-
你应该放回与答案对应的相关代码
标签: javascript html reactjs react-router react-jsx
编辑
“Warning: setState(...): Can only update amounted ormounting component”是什么意思?
【问题讨论】:
标签: javascript html reactjs react-router react-jsx
首先,像这样将所有 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
}
}
无论如何,我已经测试了你的代码,它工作正常。
【讨论】:
您可以尝试使用componentWillUnmount生命周期函数来检查组件何时被卸载。
您还可以在设置状态之前使用标志来表示组件已卸载:
saveName(nameText) {
if (!this.isUnmounted){
this.setState({submitSuccess: true});
}
}
componentWillUnmount() {
this.isUnmounted = true;
}
【讨论】: