One Logo Pool Ball
wwwwwwwwwwwwwwwwwww

Network

Read the connection type and whether the device is online, and hear when it changes

One.Network tells you how the device is connected right now (Wi-Fi, cellular, Ethernet, or not at all) and calls you back when that changes. On iOS it is NWPathMonitor; on Android it is ConnectivityManager watching the default network; on web it is navigator.onLine and the online and offline events.

Use it to show an offline state, hold work until a connection returns, or save large downloads for Wi-Fi. It reports what the system knows about the connection; it never sends a request of its own.

Usage

import { Text } from 'react-native'
import { useNetworkState } from 'one'
function OfflineBanner() {
const { type } = useNetworkState()
if (type !== 'none') return null
return <Text>You're offline. Changes will sync when you reconnect.</Text>
}

useNetworkState reads the state, subscribes to changes, and unsubscribes on unmount. Checking type === 'none' rather than isConnected matters on iOS and Android, where the hook starts as unknown before the first reading arrives.

How it works

One shared monitor. The first addStateListener starts one native monitor, and removing the last subscription stops it. Every subscriber gets the same events. Starting the monitor inside the first subscription means its first reading can never arrive before anyone is listening.

iOS. A path is connected only when NWPathMonitor calls it satisfied; anything else is none. The type is cellular, wifi, or ethernet from the interface the path uses, and unknown for any other. Both flags are true for every satisfied path. getState starts a fresh monitor, takes its first path, and cancels it.

Android. One registers a default network callback, so it follows whichever network the system routes traffic through. isConnected is the network’s internet capability and isInternetReachable is its validated capability, which Android sets once its own check gets through. The type comes from the network’s transports, checked in the order cellular, Wi-Fi, Bluetooth, Ethernet, VPN; a network with none of those is other. Losing the default network reports none. The library declares the ACCESS_NETWORK_STATE permission, which Android grants at install.

Web. A connected browser reports type unknown, because browser connection types are unreliable, and both flags follow navigator.onLine. Listeners fire on the window’s online and offline events.

The hook. On iOS and Android, useNetworkState starts as unknown with both flags false, then reads the state and subscribes. If an event lands before the read resolves, the event wins and the older read is dropped. On web it starts from the current value.

API reference

MemberReturns
getState()A promise of the current state
addStateListener(listener)A subscription
useNetworkState()The state, as React state

getState and addStateListener live on One.Network; the hook is its own export from one.

NetworkStateTypeDescription
typeNetworkStateTypeThe connection: none, unknown, cellular, wifi, bluetooth, ethernet, wimax, vpn, or other.
NetworkStateTypeThe connection: none, unknown, cellular, wifi, bluetooth, ethernet, wimax, vpn, or other.
isConnectedbooleanWhether the system has a connection that can reach the internet.
booleanWhether the system has a connection that can reach the internet.
isInternetReachablebooleanOn Android, whether the system verified it. Elsewhere, the same as isConnected.
booleanOn Android, whether the system verified it. Elsewhere, the same as isConnected.
NetworkStateSubscriptionTypeDescription
remove() => voidStop calling this listener.
() => voidStop calling this listener.

A listener that is not a function throws before anything starts. A failed read rejects with an error code: E_NETWORK_TIMEOUT on iOS when no path arrives within five seconds, and E_NETWORK_STATE on Android.

Platform differences

iOSAndroidWeb
SourcePath monitorDefault networkBrowser online flag
Named typesWi-Fi, cellular, EthernetAll but WiMAXNone
ReachableSame as connectedVerified by AndroidSame as connected
Hook startsUnknownUnknownCurrent value

Limits and gotchas

Examples

Wait for a connection

import { One } from 'one'
function whenOnline(): Promise<void> {
return new Promise((resolve) => {
const subscription = One.Network.addStateListener((state) => {
if (state.type === 'none') return
subscription.remove()
resolve()
})
One.Network.getState().then((state) => {
if (state.type === 'none') return
subscription.remove()
resolve()
})
})
}
async function sendWhenOnline(send: () => Promise<void>) {
await whenOnline()
await send()
}

The listener catches a connection that returns later, and the read covers one that is already up. Removing the subscription twice is harmless, so whichever answers first wins.

Download large files on Wi-Fi only

import { One } from 'one'
async function canDownloadLarge() {
const { type } = await One.Network.getState()
return type === 'wifi' || type === 'ethernet'
}
async function syncVideos(download: () => Promise<void>) {
if (await canDownloadLarge()) {
await download()
}
}

Reading the state just before the download avoids acting on a value from when the screen opened. On web the type is always unknown, so this function never downloads there; decide separately what the browser should do.

Show connection details

import { Text, View } from 'react-native'
import { useNetworkState } from 'one'
function ConnectionInfo() {
const { type, isConnected, isInternetReachable } = useNetworkState()
return (
<View>
<Text>Connection: {type}</Text>
<Text>Connected: {isConnected ? 'yes' : 'no'}</Text>
<Text>Verified: {isInternetReachable ? 'yes' : 'no'}</Text>
</View>
)
}

On Android, joining a Wi-Fi network that needs a sign-in page shows connected but not verified until people sign in.

Edit this page on GitHub.