有没有办法检查反应组件是否已卸载? [英] Is there a way to check if the react component is unmounted?
本文介绍了有没有办法检查反应组件是否已卸载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个用例,我需要卸载我的反应组件。但在某些情况下,特定的反应组分不会被不同的功能所替代。
因此,我需要在卸载之前检查组件是否已安装。
I have a usecase where i need to unmount my react component. But in some cases, the particular react component is unmounted by a different function. Hence, I need to check if the component is mounted before unmounting it.
推荐答案
自 isMounted()
正在被弃用,你可以在你的组件中执行此操作:
Since isMounted()
is being officially deprecated, you can do this in your component:
componentDidMount() {
this._ismounted = true;
}
componentWillUnmount() {
this._ismounted = false;
}
这种维持你自己的州的模式
变量在ReactJS文档中有详细说明: https ://facebook.github.io/react/blog/2015/12/16/ismounted-antipattern.html
This pattern of maintaining your own state
variable is detailed in the ReactJS documentation: https://facebook.github.io/react/blog/2015/12/16/ismounted-antipattern.html
这篇关于有没有办法检查反应组件是否已卸载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文