The 'react-native' application can obtain the network status of the device by using the 'NetInfo' package.

NetInfo

The NetInfo module can obtain the current network status of the device, which can be subscribed and obtained once. Previously integrated in React – Native, it has been moved separately to @React – Native – Community Management.

  • Installation (iOS requires pod install)
yarn add @react-native-community/netinfo
  • The introduction of the module
import NetInfo from '@react-native-community/netinfo'
  • Get network status at once
Netinfo.fetch (). Then (state => {console.log(" network link type ", state. Console. log(" Is the network connected?" , state.isConnected); })
  • Subscribe network status (hooks)
import {useNetInfo} from "@react-native-community/netinfo" const YourComponent = () => { const netInfo = useNetInfo() Return (<View> <Text> network type: {netinfo.type}</Text> <Text> network link? {netInfo.isConnected.toString()}</Text> </View> ); }

Official Git repository reference: https://github.com/react-native-netinfo/react-native-netinfo#usenetinfo