Skip to content
React Native Network Info API for Android & iOS
Branch: master
Clone or download
semantic-release-bot chore(release): 2.0.4 [skip ci]
## [2.0.4](v2.0.3...v2.0.4) (2019-04-17)

### Bug Fixes

* **android:** Check if NetworkInfo is null to avoid crashes ([#66](#66)) ([3137954](3137954))
Latest commit f26edd4 Apr 17, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.circleci
.github docs: Document the known issues with the iOS Simulator (#43) Apr 9, 2019
android fix(android): Check if NetworkInfo is null to avoid crashes (#66) Apr 17, 2019
example chore: Switch to the community ESLint config (#45) Apr 9, 2019
ios
js
typings
.eslintignore
.eslintrc.js chore: Switch to the community ESLint config (#45) Apr 9, 2019
.flowconfig
.gitattributes
.gitignore
.npmignore chore: Add bob to package the library (#49) Apr 9, 2019
.prettierrc
.releaserc
CONTRIBUTING.md
LICENSE
README.md
babel.config.js
jest.setup.js
package.json chore(release): 2.0.4 [skip ci] Apr 17, 2019
react-native-netinfo.podspec fix(podspec): remove slash from "name" and add missing ":tag" (#21) Feb 21, 2019
tsconfig.json
yarn.lock

README.md

@react-native-community/netinfo

CircleCI Status Supports Android and iOS MIT License

React Native Network Info API for Android & iOS. It allows you to get information on:

  • Connection type
  • Connection quality

Getting started

Install the library using either Yarn:

yarn add @react-native-community/netinfo

or npm:

npm install --save @react-native-community/netinfo

You then need to link the native parts of the library for the platforms you are using. The easiest way to link the library is using the CLI tool by running this command from the root of your project:

react-native link @react-native-community/netinfo

If you can't or don't want to use the CLI tool, you can also manually link the library using the instructions below (click on the arrow to show them):

Manually link the library on iOS

Either follow the instructions in the React Native documentation to manually link the framework or link using Cocoapods by adding this to your Podfile:

pod 'react-native-netinfo', :path => '../node_modules/@react-native-community/netinfo'
Manually link the library on Android

Make the following changes:

android/settings.gradle

include ':react-native-community-netinfo'
project(':react-native-community-netinfo').projectDir = new File(rootProject.projectDir, '../node_modules/@react-native-community/netinfo/android')

android/app/build.gradle

dependencies {
   ...
   implementation project(':react-native-community-netinfo')
}

android/app/src/main/.../MainApplication.java

On top, where imports are:

import com.reactnativecommunity.netinfo.NetInfoPackage;

Add the RNLocationPackage class to your list of exported packages.

@Override
protected List<ReactPackage> getPackages() {
    return Arrays.asList(
            new MainReactPackage(),
            new NetInfoPackage()
    );
}

Migrating from the core react-native module

This 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 you imports from:

import { NetInfo } from "react-native";

to:

import NetInfo from "@react-native-community/netinfo";

Usage

Import the library:

import NetInfo from "@react-native-community/netinfo";

Get the network state once:

NetInfo.getConnectionInfo().then(data => {
  console.log("Connection type", data.type);
  console.log("Connection effective type", data.effectiveType);
});

Subscribe to network updates:

const listener = data => {
  console.log("Connection type", data.type);
  console.log("Connection effective type", data.effectiveType);
};

// Subscribe
const subscription = NetInfo.addEventListener('connectionChange', listener);

// Unsubscribe through remove
subscription.remove();
// Or, unsubscribe through event name
NetInfo.removeEventListener('connectionChange', listener);

API

Types

NetInfoData

Describes the current state of the network. It is an object with these properties:

Property Platform Type Description
type Android, iOS NetInfoType The type of the current connection.
effectiveType Android, iOS NetInfoEffectiveType The type of cellular connection (eg. 3g, 4g, etc.). Will be unknown unless type is cellular.

NetInfoType

Describes the current type of network connection. It is an enum with these possible values:

Value Platform Description
none Android, iOS No network connection is active
unknown Android, iOS The network state could not be determined
cellular Android, iOS The active network is a cellular connection
wifi Android, iOS The active network is a Wifi connection
bluetooth Android The active network over Bluetooth
ethernet Android The active network over a wired ethernet connection
wimax Android The active network over a WiMax connection

NetInfoEffectiveType

Describes the current type of the cellular connection. It is an enum with these possible values:

Value Platform Description
unknown Android, iOS Either we are not currently connected to a cellular network or type could not be determined
2g Android, iOS We are currently connected to a 2G cellular network. Includes CDMA, EDGE, GPRS, and IDEN type connections
3g Android, iOS We are currently connected to a 3G cellular network. Includes EHRPD, EVDO, HSPA, HSUPA, HSDPA, and UTMS type connections
4g Android, iOS We are currently connected to a 4G cellular network. Includes HSPAP and LTE type connections

Methods

getConnectionInfo()

Platforms: Android, iOS

Returns a Promise that resolves to a NetInfoData object.

Example:

NetInfo.getConnectionInfo().then(data => {
  console.log("Connection type", data.type);
  console.log("Connection effective type", data.effectiveType);
});

addEventListener()

Platforms: Android, iOS

Subscribe to connection information. The callback is called a paramter of type NetInfoData whenever the connection state changes. Your listener will be called with the latest information soon after you subscribe and then with any subsiquent changes afterwards. Due to platform differences, you should not assume that the listener is called in the same way across devices or platforms.

Parameter Type Description
eventName connectionChange The event name is always connectionChange
listener (data: NetInfoData) => void The listener which will be called whenever the connection state changes

Example:

const listener = data => {
  console.log("Connection type", data.type);
  console.log("Connection effective type", data.effectiveType);
};

// Subscribe
const subscription = NetInfo.addEventListener('connectionChange', listener);

// Unsubscribe through remove
subscription.remove();
// Or, unsubscribe through event name
NetInfo.removeEventListener('connectionChange', listener);

isConnected.fetch()

Platforms: Android, iOS

Returns a Promise that resolves to a boolean which says if there is an active connection.

Note: This only says if a device has an active connection, not that it is able to reach the internet.

Getting the connection status once:

Example:

NetInfo.isConnected.fetch().then(isConnected => {
  console.log("Is connected", isConnected);
});

isConnected.addEventListener()

Platforms: Android, iOS

Calls the listener with a boolean parameter whenever the connection state change which says if there is an active connection. Your listener will be called with the latest information soon after you subscribe and then with any subsiquent changes afterwards. Due to platform differences, you should not assume that the listener is called in the same way across devices or platforms.

Note: This only says if a device has an active connection, not that it is able to reach the internet.

Parameter Type Description
eventName connectionChange The event name is always connectionChange
listener (isConnection: boolean) => void The listener which will be called whenever the connection state changes

Example:

const listener = isConnected => {
  console.log("Is connected", isConnected);
};

// Subscribe
const subscription = NetInfo.isConnected.addEventListener('connectionChange', listener);

// Unsubscribe through remove
subscription.remove();

// Unsubscribe through event name
NetInfo.isConnected.removeEventListener('connectionChange', listener);

isConnectionExpensive()

Platforms: Android

Returns a Promise which resolves to a boolean which says 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.

Example:

NetInfo.isConnectionExpensive().then(isConnectionExpensive => {
  console.log("Is connection expensive", isConnectionExpensive);
});

Known issues with the iOS simulator

There is a known issue with the iOS Simulator which causes it to not receive network change notifications correctly when the host machine disconnects and then connects to Wifi. If you are having issues with iOS then please test on an actual device before reporting any bugs.

Maintainers

Contributing

Please see the contributing guide.

License

The library is released under the MIT license. For more information see LICENSE.

You can’t perform that action at this time.