How to use the @react-native-community/netinfo.default.addEventListener function in @react-native-community/netinfo

To help you get started, we’ve selected a few @react-native-community/netinfo examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github pusher / pusher-js / src / runtimes / react-native / net_info.ts View on Github external
constructor() {
    super();
    this.online = true;

    NativeNetInfo.getConnectionInfo().then(connectionState => {
      this.online = hasOnlineConnectionState(connectionState);
    });

    NativeNetInfo.addEventListener('connectionChange', (connectionState)=>{
      var isNowOnline = hasOnlineConnectionState(connectionState);

      // React Native counts the switch from Wi-Fi to Cellular
      // as a state change. Return if current and previous states
      // are both online/offline
      if (this.online === isNowOnline) return;
      this.online = isNowOnline;
      if (this.online){
        this.emit("online");
      } else {
        this.emit("offline");
      }
    });
  }