Skip to Content
ohooks

useNetworkInformation

Tracks network connectivity status and connection quality metrics (effectiveType, downlink, rtt, saveData) via the Network Information API.

Loading preview...

The useNetworkInformation hook subscribes to online/offline events and the browser's navigator.connection (NetworkInformation) API using useSyncExternalStore, exposing live connection speed, latency, and data-saver preferences.

Installation

pnpm dlx shadcn@latest add https://ohooks.vercel.app/r/useNetworkInformation.json

Usage

Examples

Adaptive Media Quality (saveData and effectiveType)

Serve lower-resolution images or disable autoplay video when the user is on a slow connection (2g / slow-2g) or has Data Saver enabled.

Displaying Connection Diagnostics

Read downlink (Mbps) and rtt (round-trip time in ms) when isSupported is true.

API Reference

NetworkInformationState

Network connectivity and quality metrics returned by the hook.

Property
Specification
isOnline
union
Whether the browser currently reports being online (navigator.onLine)
isSupported
union
Whether the Network Information API (navigator.connection) is supported
downlink?
number
Effective bandwidth estimate in megabits per second
downlinkMax?
number
Maximum downlink speed in megabits per second for the underlying connection technology
effectiveType?
union
Effective connection type ('slow-2g', '2g', '3g', or '4g')
rtt?
number
Estimated effective round-trip time of the current connection in milliseconds
saveData?
union
Whether the user has requested a reduced data usage mode
type?
union
Type of connection a device is using to communicate with the network