useNetworkStatus
The useNetworkStatus hook tracks whether the browser is online and, where the Network Information API is available, the quality of the connection. Use it to load lighter assets on slow networks or to respect a user's data-saver setting.
Usage
Installation
import { useNetworkStatus } from "hookstorm";
Parameters
This hook does not take any parameters.
Return Values
- isOnline —
falsewhen the browser reports it is offline. - effectiveType — Estimated connection type:
"slow-2g","2g","3g"or"4g". - downlink — Estimated downlink speed in Mbps.
- rtt — Estimated round-trip time in milliseconds.
- saveData —
trueif the user enabled a data-saver mode.
Example Usage
import { ReactElement } from "react";
import { useNetworkStatus } from "hookstorm";
export default function ConnectionBadge(): ReactElement {
const { isOnline, effectiveType, saveData } = useNetworkStatus();
if (!isOnline) return <p>You are offline</p>;
return (
<p>
Connection: {effectiveType ?? "unknown"}
{saveData ? " (data saver on)" : ""}
</p>
);
}
Notes
effectiveType,downlink,rttandsaveDataareundefinedin browsers without the Network Information API (currently Safari and Firefox). Always handle that case.- If you only need online / offline,
useOnlineStatusis lighter. - SSR-safe: assumes the user is online on the server.