useNetworkbrowser
useNetwork
NewSSR SAFETracks the browser's online and offline status and provides real-time Network Information API telemetry.
$ pnpm dlx shadcn@latest add @byphongphan/use-networkRequires
@byphongphan in components.jsonuse-network.tsx
Loading preview...
Installation
$ pnpm dlx shadcn@latest add @byphongphan/use-networkRequires
@byphongphan in components.jsonUsage
Offline Status Banner
Connection Telemetry Dashboard
Features
- Reactive Online & Offline Detection: Listens to browser-level
onlineandofflineevents with passive event listeners. - Network Information API Integration: Reads connection quality properties (
downlink,rtt,effectiveType,saveData) on supported browsers (Chromium / Chrome / Edge / Opera). - Dynamic Change Tracking: Automatically recalculates telemetry whenever the underlying network adapter changes (e.g. switching between Wi-Fi and 4G/5G).
- SSR-Safe: Gracefully initializes with
{ online: true }during server rendering to prevent hydration mismatch warnings. - Zero Dependencies: Pure React hook using native Web APIs without external libraries.
Parameters
The hook takes no arguments: useNetwork(): NetworkState.
Returns
Returns a NetworkState object:
| Property | Type | Description |
|---|---|---|
online | boolean | Whether the browser has active internet connectivity (navigator.onLine). |
since | Date | undefined | Timestamp representing when the current status was recorded. |
downlink | number | undefined | Estimated effective bandwidth in megabits per second (Mbps). |
downlinkMax | number | undefined | Maximum downlink speed supported by the physical connection technology. |
effectiveType | 'slow-2g' | '2g' | '3g' | '4g' | string | undefined | Effective connection type derived from latency and bandwidth calculations. |
rtt | number | undefined | Estimated round-trip latency in milliseconds (ms). |
saveData | boolean | undefined | Indicates whether the user has requested a reduced-data mode in browser. |
type | string | undefined | Physical connection type ('wifi', 'cellular', 'ethernet', etc.). |