NetInfo
NetInfo exposes info about online/offline status
NetInfo.fetch().done((reach) => {
console.log('Initial: ' + reach);
});
function handleFirstConnectivityChange(reach) {
console.log('First change: ' + reach);
NetInfo.removeEventListener(
'change',
handleFirstConnectivityChange
);
}
NetInfo.addEventListener(
'change',
handleFirstConnectivityChange
);
IOS
Asynchronously determine if the device is online and on a cellular network.
none
- device is offlinewifi
- device is online and connected via wifi, or is the iOS simulatorcell
- device is connected via Edge, 3G, WiMax, or LTEunknown
- error case and the network status is unknown
Android
To request network info, you need to add the following line to your app's AndroidManifest.xml
:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Asynchronously determine if the device is connected and details about that connection.
Android Connectivity Types.
NONE
- device is offlineBLUETOOTH
- The Bluetooth data connection.DUMMY
- Dummy data connection.ETHERNET
- The Ethernet data connection.MOBILE
- The Mobile data connection.MOBILE_DUN
- A DUN-specific Mobile data connection.MOBILE_HIPRI
- A High Priority Mobile data connection.MOBILE_MMS
- An MMS-specific Mobile data connection.MOBILE_SUPL
- A SUPL-specific Mobile data connection.VPN
- A virtual network using one or more native bearers. Requires API Level 21WIFI
- The WIFI data connection.WIMAX
- The WiMAX data connection.UNKNOWN
- Unknown data connection.
The rest ConnectivityStates are hidden by the Android API, but can be used if necessary.
isConnectionExpensive
Available on Android. Detect if the current active connection is metered or not. A network is classified as metered when the user is sensitive to heavy data usage on that connection due to monetary costs, data limitations or battery/performance issues.
NetInfo.isConnectionExpensive()
.then(isConnectionExpensive => {
console.log('Connection is ' + (isConnectionExpensive ? 'Expensive' : 'Not Expensive'));
})
.catch(error => {
console.error(error);
});
isConnected
Available on all platforms. Asynchronously fetch a boolean to determine internet connectivity.
NetInfo.isConnected.fetch().then(isConnected => {
console.log('First, is ' + (isConnected ? 'online' : 'offline'));
});
function handleFirstConnectivityChange(isConnected) {
console.log('Then, is ' + (isConnected ? 'online' : 'offline'));
NetInfo.isConnected.removeEventListener(
'change',
handleFirstConnectivityChange
);
}
NetInfo.isConnected.addEventListener(
'change',
handleFirstConnectivityChange
);
Methods
Properties
Reference
Methods
addEventListener()
static addEventListener(eventName, handler)
Invokes the listener whenever network status changes. The listener receives one of the connectivity types listed above.
removeEventListener()
static removeEventListener(eventName, handler)
Removes the listener for network status changes.
fetch()
static fetch()
Returns a promise that resolves with one of the connectivity types listed above.
isConnectionExpensive()
static isConnectionExpensive()