CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-react-native-community--netinfo

React Native Network Info API for iOS & Android

Pending
Overview
Eval results
Files

network-state.mddocs/

Network State Management

Core network information functionality for retrieving connection status, connection type, and internet reachability. Supports both one-time queries and continuous monitoring with a global singleton pattern.

Capabilities

Fetch Network State

Returns a Promise that resolves to the current network state information.

/**
 * Returns a Promise that resolves to a NetInfoState object.
 * This function operates on the global singleton instance configured using configure()
 * @param requestedInterface - interface from which to obtain the information
 * @returns A Promise which contains the current connection state
 */
function fetch(requestedInterface?: string): Promise<NetInfoState>;

Usage Examples:

import { fetch } from "@react-native-community/netinfo";

// Get current network state
const state = await fetch();
console.log("Connection type:", state.type);
console.log("Is connected:", state.isConnected);
console.log("Is internet reachable:", state.isInternetReachable);

// Request specific interface (optional)
const wifiState = await fetch("wifi");

Refresh Network State

Force-refreshes the internal state of the global singleton and returns updated network information.

/**
 * Force-refreshes the internal state of the global singleton managed by this library.
 * @returns A Promise which contains the updated connection state
 */
function refresh(): Promise<NetInfoState>;

Usage Examples:

import { refresh } from "@react-native-community/netinfo";

// Force refresh network state
const freshState = await refresh();
console.log("Updated connection type:", freshState.type);

Event Listener

Subscribe to network state changes. The callback is called whenever the connection state changes.

/**
 * Subscribe to the global singleton's connection information. The callback is called with a parameter of type
 * NetInfoState whenever the connection state changes. Your listener
 * will be called with the latest information soon after you subscribe and then with any
 * subsequent changes afterwards. You should not assume that the listener is called in the same
 * way across devices or platforms.
 * @param listener - The listener which is called when the network state changes
 * @returns A function which can be called to unsubscribe
 */
function addEventListener(listener: NetInfoChangeHandler): NetInfoSubscription;

Usage Examples:

import { addEventListener } from "@react-native-community/netinfo";

// Subscribe to network changes
const unsubscribe = addEventListener(state => {
  console.log("Network changed:", state.type);
  
  if (state.isConnected) {
    console.log("Connected to:", state.type);
    if (state.type === 'wifi' && state.details) {
      console.log("WiFi SSID:", state.details.ssid);
    }
  } else {
    console.log("Disconnected");
  }
});

// Clean up subscription
unsubscribe();

Configuration

Configures the library with global configuration options. Note that calling this will stop all previously added listeners from being called again.

/**
 * Configures the library with the given configuration. Note that calling this will stop all
 * previously added listeners from being called again. It is best to call this right when your
 * application is started to avoid issues. The configuration sets up a global singleton instance.
 * @param configuration - The new configuration to set
 */
function configure(configuration: Partial<NetInfoConfiguration>): void;

Usage Examples:

import { configure } from "@react-native-community/netinfo";

// Configure custom reachability testing
configure({
  reachabilityUrl: 'https://clients3.google.com/generate_204',
  reachabilityTest: async (response) => response.status === 204,
  reachabilityLongTimeout: 60 * 1000, // 60s
  reachabilityShortTimeout: 5 * 1000, // 5s
  reachabilityRequestTimeout: 15 * 1000, // 15s
  shouldFetchWiFiSSID: true, // iOS: requires meeting requirements
  useNativeReachability: false
});

Network State Types

interface NetInfoUnknownState {
  type: NetInfoStateType.unknown;
  isConnected: boolean | null;
  isInternetReachable: null;
  details: null;
  isWifiEnabled?: boolean;
}

type NetInfoNoConnectionState = {
  type: NetInfoStateType.none;
  isConnected: boolean;
  isInternetReachable: boolean;
  details: null;
  isWifiEnabled?: boolean;
};

type NetInfoCellularState = {
  type: NetInfoStateType.cellular;
  isConnected: boolean;
  isInternetReachable: boolean | null;
  details: {
    isConnectionExpensive: boolean;
    cellularGeneration: NetInfoCellularGeneration | null;
    carrier: string | null;
  };
  isWifiEnabled?: boolean;
};

type NetInfoWifiState = {
  type: NetInfoStateType.wifi;
  isConnected: boolean;
  isInternetReachable: boolean | null;
  details: {
    isConnectionExpensive: boolean;
    ssid: string | null;
    bssid: string | null;
    strength: number | null;
    ipAddress: string | null;
    subnet: string | null;
    frequency: number | null;
    linkSpeed: number | null;
    rxLinkSpeed: number | null;
    txLinkSpeed: number | null;
  };
  isWifiEnabled?: boolean;
};

type NetInfoBluetoothState = {
  type: NetInfoStateType.bluetooth;
  isConnected: boolean;
  isInternetReachable: boolean | null;
  details: {
    isConnectionExpensive: boolean;
  };
  isWifiEnabled?: boolean;
};

type NetInfoEthernetState = {
  type: NetInfoStateType.ethernet;
  isConnected: boolean;
  isInternetReachable: boolean | null;
  details: {
    isConnectionExpensive: boolean;
    ipAddress: string | null;
    subnet: string | null;
  };
  isWifiEnabled?: boolean;
};

type NetInfoWimaxState = {
  type: NetInfoStateType.wimax;
  isConnected: boolean;
  isInternetReachable: boolean | null;
  details: {
    isConnectionExpensive: boolean;
  };
  isWifiEnabled?: boolean;
};

type NetInfoVpnState = {
  type: NetInfoStateType.vpn;
  isConnected: boolean;
  isInternetReachable: boolean | null;
  details: {
    isConnectionExpensive: boolean;
  };
  isWifiEnabled?: boolean;
};

type NetInfoOtherState = {
  type: NetInfoStateType.other;
  isConnected: boolean;
  isInternetReachable: boolean | null;
  details: {
    isConnectionExpensive: boolean;
  };
  isWifiEnabled?: boolean;
};

type NetInfoDisconnectedStates = NetInfoUnknownState | NetInfoNoConnectionState;
type NetInfoConnectedStates = 
  | NetInfoCellularState 
  | NetInfoWifiState 
  | NetInfoBluetoothState 
  | NetInfoEthernetState 
  | NetInfoWimaxState 
  | NetInfoVpnState 
  | NetInfoOtherState;

type NetInfoState = NetInfoDisconnectedStates | NetInfoConnectedStates;

type NetInfoChangeHandler = (state: NetInfoState) => void;
type NetInfoSubscription = () => void;

Install with Tessl CLI

npx tessl i tessl/npm-react-native-community--netinfo

docs

configuration.md

hooks.md

index.md

network-state.md

tile.json