React Native Network Info API for Android & iOS
React Native Network Info API for Android & iOS
@react-native-community/netinfoReact Native Network Info API for Android, iOS, macOS, Windows & Web. It allows you to get information on:
Install the library using either Yarn:
yarn add @react-native-community/netinfoor npm:
npm install --save @react-native-community/netinfoLinking the package manually is not required anymore with Autolinking.
iOS Platform:
$ npx pod-install # CocoaPods on iOS needs this extra step
Android Platform with AndroidX:
Modify your android/build.gradle configuration:
buildscript {
ext {
buildToolsVersion = "xx.yy.zz"
minSdkVersion = xyz
compileSdkVersion = xyz
targetSdkVersion = xyz
}macOS Platform:
Autolinking is not yet available on macOS. See the Manual linking steps for macOS below.
Open your project .xcodeproj on xcode.
Right click on the Libraries folder and select Add files to "yourProjectName".
Add RNCNetInfo.xcodeproj (located at node_modules/@react-native-community/react-native-netinfo/macos) to your project Libraries.
Go to Build Phases -> Link Binary with Libraries and add: libRNCNetInfo-macOS.a.
Windows Platform:
Autolinking automatically works on RNW >= 0.63.
To use this library you need to ensure you are using the correct version of React Native. We support react-native 0.60+ with auto-linking.
If you are using a version of React Native that is lower than 0.60 check older versions of this README for details, but no support will be provided.
The web implementation heavily depends on the Network Information API which is still an is an experimental technology and thus it's not supported in every browser. If this API is not available the library will safely fallback to the old onLine property and return basic connection information.
AbortController is used to cancel network requests, and may not be available on Internet Explorer, though it is available on Edge https://caniuse.com/abortcontroller
Node v16 is the minimum required node version - AbortController is only present in stable versions of node from v16 on
react-native moduleThis module was created when the NetInfo was split out from the core of React Native. To migrate to this module you need to follow the installation instructions above and then change your imports from:
import { NetInfo } from "react-native";to:
import NetInfo from "@react-native-community/netinfo";Note that the API was updated after it was extracted from NetInfo to support some new features, however, the previous API is still available and works with no updates to your code.
Internally this library has a network state manager class to handle all the functionality and state. This library provides two options for instantiating the class:
Subscribe to network state updates:
import { addEventListener } from "@react-native-community/netinfo";
// Subscribe
const unsubscribe = addEventListener(state => {
console.log("Connection type", state.type);
console.log("Is connected?", state.isConnected);
});
// Unsubscribe
unsubscribe();Get the network state once:
import { fetch } from "@react-native-community/netinfo";
fetch().then(state => {
console.log("Connection type", state.type);
console.log("Is connected?", state.isConnected);
});Get network state updates from the global instance via a react hook:
import { useNetInfo } from "@react-native-community/netinfo";
const { type, isConnected } = useNetInfo();Use an isolated instance of the network manager:
import { useNetInfoInstance } from "@react-native-community/netinfo";
const { netInfo: { type, isConnected }, refresh } = useNetInfoInstance();NetInfoStateDescribes the current state of the network. It is an object with these properties:
| Property | Type | Description |
|---|---|---|
type |
NetInfoStateType |
The type of the current connection. |
isConnected |
boolean, null |
If there is an active network connection. Defaults to null on most platforms for unknown networks. Note: Web browsers report network type unknown for many otherwise valid networks (https://caniuse.com/netinfo), so isConnected may be true or false and represent a real connection status even for unknown network types in certain cases. |
isInternetReachable |
boolean, null |
If the internet is reachable with the currently active network connection. If unknown defaults to null |
isWifiEnabled |
boolean |
(Android only) Whether the device's WiFi is ON or OFF. |
details |
The value depends on the type value. See below. |
The details value depends on the type value.
type is none or unknowndetails is null.
type is wifidetails has these properties:
| Property | Platform | Type | Description |
|---|---|---|---|
isConnectionExpensive |
Android, iOS, macOS, Windows, Web | boolean |
If the network connection is considered "expensive". This could be in either energy or monetary terms. |
ssid |
Android, iOS (not tvOS), Windows | string |
The SSID of the network. May not be present, null, or an empty string if it cannot be determined. On iOS, your app must meet at least one of the following requirements and you must set the shouldFetchWiFiSSID configuration option or no attempt will be made to fetch the SSID. On Android, you need to have the ACCESS_FINE_LOCATION permission in your AndroidManifest.xml and accepted by the user. |
bssid |
Android, iOS (not tvOS), Windows* | string |
The BSSID of the network. May not be present, null, or an empty string if it cannot be determined. On iOS, make sure your app meets at least one of the following requirements. On Android, you need to have the ACCESS_FINE_LOCATION permission in your AndroidManifest.xml and accepted by the user. |
strength |
Android, Windows | number |
An integer number from 0 to 100 for the signal strength. May not be present if the signal strength cannot be determined. |
ipAddress |
Android, iOS, macOS, Windows | string |
The external IP address. Can be in IPv4 or IPv6 format. May not be present if it cannot be determined. |
subnet |
Android, iOS, macOS | string |
The subnet mask in IPv4 format. May not be present if it cannot be determined. |
frequency |
Android, Windows* | number |
Network frequency. Example: For 2.4 GHz networks, the method will return 2457. May not be present if it cannot be determined. |
linkSpeed |
Android | number |
The link speed in Mbps. |
rxLinkSpeed |
Android | number |
The current receive link speed in Mbps. (Android Q / API level 29 and above) |
txLinkSpeed |
Android | number |
The current transmit link speed in Mbps. (Android Q / API level 29 and above) |
* Requires wiFiControl capability in appxmanifest. Without it, these values will be null.
type is cellulardetails has these properties:
| Property | Platform | Type | Description |
|---|---|---|---|
isConnectionExpensive |
Android, iOS, macOS, Windows, Web | boolean |
If the network connection is considered "expensive". This could be in either energy or monetary terms. |
cellularGeneration |
Android, iOS, Windows | NetInfoCellularGeneration |
The generation of the cell network the user is connected to. This can give an indication of speed, but no guarantees. |
carrier |
Android, iOS | string |
The network carrier name. May not be present or may be empty if none can be determined. |
type is bluetooth, ethernet, wimax, vpn, or otherdetails has these properties:
| Property | Type | Description |
|---|
No open issues yet, or sync has not completed.