【发布时间】:2018-07-01 10:20:33
【问题描述】:
在 iOs 模拟器中运行我收到错误提示
ExceptionsManager.js:73 警告:只能更新已安装或已安装的组件。这通常意味着您在未安装的组件上调用了 setState、replaceState 或 forceUpdate。这是无操作的。
在执行此操作时。
constructor(props) {
super(props);
this.state = {
isOnline: false
}
}
componentDidMount(){
NetInfo.isConnected.addEventListener('connectionChange', this.checkConnection);
}
componentWillUpdate(){
NetInfo.removeEventListener('connectionChange', this.checkConnection);
}
checkConnection = () => {
NetInfo.isConnected.fetch().then((data) => {
console.log('refresh:', data);
this.setState({
isOnline: data <------ this is the line that affect me
})
});
}
知道为什么吗?
【问题讨论】:
-
你不应该删除
componentWillUnmount钩子中的事件监听器,而不是componentWillUpdate吗?
标签: reactjs react-native components