javascript - 应用程序关闭时是否删除了侦听器?
问题描述
假设我在以下位置注册了以下侦听器componentDidMount
:
NetInfo.isConnected.addEventListener('connectionChange', this.props.connectionChange)
我需要该侦听器在应用程序运行时始终处于活动状态。
在这种情况下,是否需要调用removeEventListener
或关闭应用程序后将其删除?
解决方案
您可以componentWillUnmount
在状态为inactive
componentDidMount() {
AppState.addEventListener('change', this.handleAppStateChange);
}
componentWillUnmount() {
//remove listener here
AppState.removeEventListener('change', this.handleAppStateChange);
}
handleAppStateChange = (nextAppState) => {
if (nextAppState === 'inactive') {
//remove listener here
}
}
希望这能解决您的问题
推荐阅读
- html - 单击时按钮未关闭
- vhdl - UART 发送器仅在嵌入式逻辑分析仪运行时起作用
- windows - 我如何知道我的 MFC 应用程序是否使用 COM / OLE?
- html - 无法在 HTML 中对齐下拉菜单和文本区域
- reactjs - 如何修复为类型号返回 NaN 且不为 mongodb 中的字符串类型返回值的发布数据?
- python - 如果 pandas 中另一个数据框的列名中存在行名,则选择数据框的行值
- php - 如何将参数从播种机传递给工厂?
- python - 如何在 plotly 中制作动画 3D 散点图
- python - Python 读取日期时间字符串(使用 python 读取 T01:00:00-07:00)
- java - 在我的布局中有recyclerview和底部导航栏,但导航栏位于recycler view的后面