How to check internet connection in React Native application for both iOS and Android?

后端 未结 7 2315
孤城傲影
孤城傲影 2021-02-07 12:52

I have a React Native application and I\'m seeking to add functionality that checks if there is an active internet connection when the app first starts up, and continuously ther

7条回答
  •  萌比男神i
    2021-02-07 13:32

    I ran into this today and found solution which I believe is the best. Its gonna continuously search for network changes and display them accordingly.

    I tested it with expo install @react-native-community/netinfo and its working flawlessly.

    import {useNetInfo} from "@react-native-community/netinfo";
    
    const YourComponent = () => {
      const netInfo = useNetInfo();
    
      return (
        
          Type: {netInfo.type}
          Is Connected? {netInfo.isConnected.toString()}
        
      );
    };
    

提交回复
热议问题