Skip to content

How do i listen for change in connection status?  #6

@RuthenicEye

Description

@RuthenicEye

I did something like below..

import React, { Component } from 'react';
import { Alert } from 'react-native';
import { hasInternetConnection } from 'react-native-connection-status';
import SwitchNavigator from './navigator/AppNavigator';

export default class App extends Component<> {
  constructor() {
    super();
    this.state = {
      connectionStatus: 'Internet connection available.'
    };
  }
  componentDidMount() {
    this.checkConnection();
  }
  //Checking Internet Connectivity
  checkConnection = () => {
    hasInternetConnection((isConnected: boolean) => {
      console.log(isConnected);
      if (!isConnected) {
        Alert.alert(
          'Unexpected error',
          'Not connected to internet',
          [
            { text: 'Cancel', onPress: () => console.log('Cancel Pressed'), style: 'cancel' },
            { text: 'OK', onPress: () => console.log('OK Pressed') }
          ],
          { cancelable: false }
        );
        this.setState({
          connectionStatus: 'Internet connection not available.'
        });
      }
      console.log(this.state.connectionStatus);
    });
  };

  render() {
    return <SwitchNavigator />;
  }
}

It checks for internet only once.. i want it to listen to change in connectivity. How do i do that?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions