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

后端 未结 7 2224
孤城傲影
孤城傲影 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条回答
  •  北荒
    北荒 (楼主)
    2021-02-07 13:13

    Create NetworkUtills.js

    import NetInfo from "@react-native-community/netinfo";
    export default class NetworkUtils {
      static async isNetworkAvailable() {
        const response = await NetInfo.fetch();
        return response.isConnected;
    }}
    

    Use anywhere like this

    const isConnected = await NetworkUtils.isNetworkAvailable()
    

提交回复
热议问题