【问题标题】:NetInfo status update errorNetInfo 状态更新错误
【发布时间】: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


【解决方案1】:

ZeroBased_IX 提到:

改为这样做:

componentWillUnmount(){
  NetInfo.isConnected.removeEventListener('connectionChange');
}

它在该行中触发,因为这是您在代码库的其余部分中使用更新值的行。因此在包编译时打破它。

希望对您有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-15
    • 2021-02-03
    • 2019-07-31
    • 2018-10-03
    • 1970-01-01
    • 1970-01-01
    • 2022-11-09
    • 2022-08-17
    相关资源
    最近更新 更多