React Native Network Info API for iOS & Android
npx @tessl/cli install tessl/npm-react-native-community--netinfo@11.4.0React Native NetInfo provides a comprehensive API for accessing network information on iOS, Android, macOS, Windows, and Web platforms. It enables developers to retrieve connection type and connection quality information through a unified API across all supported platforms with both global singleton and isolated instance patterns.
npm install @react-native-community/netinfoimport NetInfo from "@react-native-community/netinfo";Or use named imports:
import {
configure,
fetch,
addEventListener,
useNetInfo,
NetInfoState,
NetInfoStateType
} from "@react-native-community/netinfo";For CommonJS:
const NetInfo = require("@react-native-community/netinfo");import NetInfo from "@react-native-community/netinfo";
// Get network state once
const state = await NetInfo.fetch();
console.log("Connection type", state.type);
console.log("Is connected?", state.isConnected);
// Subscribe to network state updates
const unsubscribe = NetInfo.addEventListener(state => {
console.log("Connection type", state.type);
console.log("Is connected?", state.isConnected);
});
// Unsubscribe
unsubscribe();React Native NetInfo is built around several key components:
Core network information functionality for retrieving connection status, connection type, and internet reachability. Supports both one-time queries and continuous monitoring.
function fetch(requestedInterface?: string): Promise<NetInfoState>;
function refresh(): Promise<NetInfoState>;
function addEventListener(listener: NetInfoChangeHandler): NetInfoSubscription;
function configure(configuration: Partial<NetInfoConfiguration>): void;React hooks for integrating network state into components with automatic re-rendering on network changes. Includes both global singleton and isolated instance patterns.
function useNetInfo(
configuration?: Partial<NetInfoConfiguration>
): NetInfoState;
function useNetInfoInstance(
isPaused?: boolean,
configuration?: Partial<NetInfoConfiguration>
): {
netInfo: NetInfoState;
refresh: () => void;
};Configuration system for customizing network reachability testing, including custom endpoints, timeout settings, and platform-specific options.
interface NetInfoConfiguration {
reachabilityUrl: string;
reachabilityMethod?: NetInfoMethodType;
reachabilityHeaders?: Record<string, string>;
reachabilityTest: (response: Response) => Promise<boolean>;
reachabilityLongTimeout: number;
reachabilityShortTimeout: number;
reachabilityRequestTimeout: number;
reachabilityShouldRun: () => boolean;
shouldFetchWiFiSSID: boolean;
useNativeReachability: boolean;
}enum NetInfoStateType {
unknown = 'unknown',
none = 'none',
cellular = 'cellular',
wifi = 'wifi',
bluetooth = 'bluetooth',
ethernet = 'ethernet',
wimax = 'wimax',
vpn = 'vpn',
other = 'other'
}
enum NetInfoCellularGeneration {
'2g' = '2g',
'3g' = '3g',
'4g' = '4g',
'5g' = '5g'
}
type NetInfoState = NetInfoDisconnectedStates | NetInfoConnectedStates;
type NetInfoChangeHandler = (state: NetInfoState) => void;
type NetInfoSubscription = () => void;
type NetInfoMethodType = 'HEAD' | 'GET';