简体   繁体   English

“警告:setState(…):只能更新已安装或正在安装的组件”是什么意思?

[英]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 . 第三,当你使用cloneElementthis.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.

相关问题 反应警告:setState(…)只能更新已安装或正在安装的组件 - React Warning: setState(…) Can only update a mounted or mounting component 警告:setState(…):只能更新已安装或正在安装的组件 - Warning react : setState(…): Can only update a mounted or mounting component 警告:setState(…):只能更新已安装或正在安装的组件,如何卸载? - Warning: setState(…): Can only update a mounted or mounting component, How to unmount? 警告:setState(...):只能更新已安装或安装的组件 - Warning: setState(…): Can only update a mounted or mounting component 反应警告setState(…):只能更新已安装或正在安装的组件 - React warning setState(…): Can only update a mounted or mounting component setState只能更新已安装或安装的组件 - setState Can only update a mounted or mounting component setState(…):只能更新已安装或正在安装的组件 - setState(…): Can only update a mounted or mounting component setState(…):即使已安装组件,也只能更新已安装或正在安装的组件 - setState(…): Can only update a mounted or mounting component even if component is mounted Warning.js:45警告:setState(…):只能更新已安装或正在安装的组件 - Warning.js:45 Warning: setState(…): Can only update a mounted or mounting component 警告:setState(…):只能更新已安装或正在安装的组件。 这通常意味着 - Warning: setState(…): Can only update a mounted or mounting component. This usually means
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM