@exodus/netinfo

中文文档
1.4.2-exodus.1发布于 2026/11/05MITTypeScriptCommonJS零依赖近 30 天下载 1.4万

React Native Network Info API for iOS & Android

原始使用文档
文档目录10 节
  1. Getting started
  2. Migrating from the core react-native module
  3. Usage
  4. Get connection info
  5. Subscribe to connection info
  6. Is connected
  7. Is connection expensive (Android only)
  8. Maintainers
  9. Contributing
  10. License

@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:

  • Online/offline status
  • 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

Start by importing the library:

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

Get connection info

Returns a promise that resolves to an object with type and effectiveType keys whose values are a ConnectionType and an EffectiveConnectionType), respectively.

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

ConnectionType

Cross platform values:

  • none - Device is offline
  • wifi - Device is online and connected via wifi, or is the iOS simulator
  • cellular - Device is connected via Edge, 3G, WiMax, or LTE
  • unknown - Error case and the network status is unknown

Android-only values:

  • bluetooth - Device is connected via Bluetooth
  • ethernet - Device is connected via Ethernet
  • wimax - Device is connected via WiMAX

EffectiveConnectionType

Cross platform values:

  • 2g
  • 3g
  • 4g
  • unknown

Subscribe to connection info

Subscribe to connection information. The callback is called whenever the connection status changes. The returned object shape is the same as getConnectionInfo above.

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

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

// Unsubscribe through remove
subscription.remove();

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

Is connected

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:

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

Or subscribing to changes:

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);

Is connection expensive (Android only)

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("Is connection expensive", isConnectionExpensive);
});

Maintainers

Contributing

Please see the contributing guide.

License

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

版本与发布记录

@exodus/netinfo 共发布 3 个版本,首次发布于 2020/07/08;过去 12 个月发布了 2 个版本。

版本发布日期距今
1.4.2-exodus.1latest2026/11/059 天前
1.4.2-exodus.02026/04/276 个月前
1.4.22020/07/086 年前

关于 @exodus/netinfo 的常见问题

@exodus/netinfo 是什么?

React Native Network Info API for iOS & Android

如何安装 @exodus/netinfo?

在项目目录运行 npm install @exodus/netinfo,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(^16.0)、react-native(>=0.57 <0.59)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@exodus/netinfo 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@exodus/netinfo 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@exodus/netinfo 可以商用吗?

@exodus/netinfo 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@exodus/netinfo 的下载量有多少?

根据 npm 官方下载统计,@exodus/netinfo 在 2026/10/14 – 2026/11/12 这 30 天内被下载 14,210 次,日均约 474 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@exodus/netinfo 还在维护吗?

最新版本 1.4.2-exodus.1 发布于 2026/11/05(9 天前);至今共发布 3 个版本,其中过去 12 个月发布了 2 个;首次发布于 2020/07/08。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。