Custom hook that tracks whether the browser is online.
Backed by navigator.onLine and the window online/offline events.
The browser can report true while the network is effectively unusable
(captive portals, flaky DNS), so treat true as a hint. false is
trustworthy: the browser is certain it has no network connection.
Transitions are reported only after the value has been stable for 500ms.
Browsers (Safari especially) fire online/offline in rapid alternating
bursts during network transitions; reacting to every event storms the tree
with synchronous re-renders and can trip React's nested-update limit.
Blips shorter than the settle window are never reported. The value read on
mount is not delayed.
Returns boolean
Whether the browser reports being online.
Example
constisOnline = useOnline();
if (!isOnline) { return <Paragraph>You're offline</Paragraph>; }
Custom hook that tracks whether the browser is online.
Backed by
navigator.onLineand the windowonline/offlineevents. The browser can reporttruewhile the network is effectively unusable (captive portals, flaky DNS), so treattrueas a hint.falseis trustworthy: the browser is certain it has no network connection.Transitions are reported only after the value has been stable for 500ms. Browsers (Safari especially) fire
online/offlinein rapid alternating bursts during network transitions; reacting to every event storms the tree with synchronous re-renders and can trip React's nested-update limit. Blips shorter than the settle window are never reported. The value read on mount is not delayed.