我应该如何清除 componentWillUnmount 中的状态? [英] How should I clear state in componentWillUnmount?

查看:26
本文介绍了我应该如何清除 componentWillUnmount 中的状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设置如下代码.

componentWillUnmount() { 
    this.setState({'modal':false}) or this.setState({})
}

但是状态不清楚.我怎样才能做到这一点?离开组件时我需要清除状态.

But state is not clear. How can I do this? I need clear state when leave component.

推荐答案

根据 Facebook 的人的说法,不应在 componentWillUnmount 上调用 setState,因为组件即将被销毁并且永远不会重新安装.如果您只是想清除"状态,则没有必要这样做,因为组件的任何新实例都将具有清晰的初始状态.

According to people at Facebook, setState should not be called on componentWillUnmount as the component is about to be destroyed and never remounted. If you just want to "clear" the state, then there is no need for that, as any new instance of the component will have the clear initial state.

请参阅此处讨论的任何问题.

See here any issue where this is talked about.

这篇关于我应该如何清除 componentWillUnmount 中的状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆