How to use @react-native-community/netinfo - 10 common examples

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");
      }
    });
  }
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");
      }
github zulip / zulip-mobile / src / boot / AppEventHandlers.js View on Github external
componentDidMount() {
    const { dispatch } = this.props;
    handleInitialNotification(dispatch);

    this.netInfoDisconnectCallback = NetInfo.addEventListener(this.handleConnectivityChange);
    AppState.addEventListener('change', this.handleAppStateChange);
    AppState.addEventListener('memoryWarning', this.handleMemoryWarning);
    SafeArea.getSafeAreaInsetsForRootView().then(params =>
      dispatch(initSafeAreaInsets(params.safeAreaInsets)),
    );
    // $FlowFixMe: libdef wrongly says callback's parameter is optional
    Orientation.addOrientationListener(this.handleOrientationChange);
    this.notificationListener.start();
  }
github digidem / mapeo-mobile / src / frontend / screens / SyncModal / index.js View on Github external
ssid = await NetworkInfo.getSSID();
      } catch (e) {
        bugsnag.notify(e);
      } finally {
        // Even if we don't get the SSID, we still want to show that a wifi
        // network is connected.
        setSsid(ssid);
      }
    };
    // When the modal opens, start announcing this device as available for sync
    api.syncJoin(deviceName);
    // Subscribe to peer updates
    subscriptions.push(api.addPeerListener(setServerPeers));
    // Subscribe to NetInfo to know when the user connects/disconnects to wifi
    subscriptions.push({
      remove: NetInfo.addEventListener(handleConnectionChange)
    });
    // Keep the screen awake whilst on this screen
    KeepAwake.activate();
    return () => {
      // When the modal closes, stop announcing for sync
      api.syncLeave();
      // Unsubscribe all listeners
      subscriptions.forEach(s => s.remove());
      // Turn off keep screen awake
      KeepAwake.deactivate();
    };
  }, []);
github rgommezz / react-native-offline / src / components / NetworkConnectivity.js View on Github external
async componentDidMount() {
    const { pingInterval } = this.props;
    const handler = this.getConnectionChangeHandler();

    NetInfo.isConnected.addEventListener('connectionChange', handler);
    // On Android the listener does not fire on startup
    if (Platform.OS === 'android') {
      const netConnected = await NetInfo.isConnected.fetch();
      handler(netConnected);
    }
    if (pingInterval > 0) {
      connectivityInterval.setup(this.intervalHandler, pingInterval);
    }
  }
github jayesbe / react-native-cacheable-image / image.js View on Github external
componentWillMount() {
        if (this.props.checkNetwork) {
            NetInfo.isConnected.addEventListener('connectionChange', this._handleConnectivityChange);
            // componentWillUnmount unsets this._handleConnectivityChange in case the component unmounts before this fetch resolves
            NetInfo.isConnected.fetch().done(this._handleConnectivityChange);
        }

        this._processSource(this.props.source, true);
    }
github pct-org / native-app / app / modules / IpFinder / IpFinder.js View on Github external
async componentDidMount() {
    const state = await NetInfo.fetch()

    let newState = {}
    newState.connectedToWifi = state.type === 'wifi' && state.isConnected

    let host = await AsyncStorage.getItem('@ipFinder.host')

    // Check if we have host or that current is still available and if we are connected to wifi
    // other check for host
    if ((!host || !(await this.isHost(host))) && newState.connectedToWifi) {
      host = null

      SplashScreen.hide()

      if (await deviceInfo.isEmulator()) {
        // If we are a emulator then check the host ip
        if (await this.isHost('10.0.2.2:5000')) {
github celo-org / celo-monorepo / packages / mobile / src / networkInfo / saga.ts View on Github external
return eventChannel((emit) => {
    return NetInfo.addEventListener((state) => emit(state))
  })
}
github expo / expo / apps / native-component-list / src / screens / NetInfoScreen.tsx View on Github external
componentDidMount() {
    this._eventCounter = 0;

    NetInfo.getConnectionInfo()
      .then(connectionInfo => this.setState({ connectionInfo }))
      .catch(console.warn);
    this._ensureIsConnectionExpensiveIsUpToDate();
    this._subscription = NetInfo.addEventListener(
      'connectionChange',
      this._handleConnectionChange
    ) as unknown as Subscription;
    this._isConnectedSubscription = NetInfo.isConnected.addEventListener(
      'connectionChange',
      this._handleIsConnectedChange
    ) as unknown as Subscription;
  }
github search-future / miyou.tv / src / utils / init / index.ts View on Github external
export default function init(store: Store) {
  BackHandler.addEventListener("hardwareBackPress", () => {
    const { nav }: { nav: NavigationState } = store.getState();
    const { index } = nav;
    if (index > 1) {
      store.dispatch(StackActions.pop({}));
      return true;
    }
    return false;
  });

  NetInfo.fetch().then(state => {
    store.dispatch(NetworkActions.update(state));
  });
  NetInfo.addEventListener(state => {
    if (typeof state === "object") {
      store.dispatch(NetworkActions.update(state));
    }
  });

  common(store);
}