useOnline

A hook that tracks and returns the browser's current online connectivity status.

Caution

A true value only indicates that the device is connected to a network; it does not guarantee actual internet access or server reachability.

Installation

bash

Description

The useOnline hook utilizes useSyncExternalStore to subscribe to the browser's online and offline window events. It provides a real-time reactive boolean based on the navigator.onLine API, ensuring that the component stays in sync with the network state and providing a safe default for Server-Side Rendering (SSR) to avoid hydration mismatches.

Return values

NameTypeDescription
onlinebooleanReturns true if the browser is online, and false otherwise. Note that true doesn't necessarily guarantee internet access, only a connection to a network.

Demo

Connectivity Monitor

Status: 🌐 OnlineYou are connected to the network. Proceed with requests.

Source code

tsx