npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@usefy/use-network-state

v0.25.1

Published

A React hook for tracking online/offline status and the Network Information API (effectiveType, downlink, saveData) with SSR support

Downloads

1,054

Readme


Overview

useNetworkState is part of the @usefy ecosystem — a collection of production-ready, TypeScript-first, SSR-safe React hooks. It combines navigator.onLine with the Network Information API (navigator.connection) to give you a single, reactive snapshot of the device's connectivity and connection quality.

Features

  • Online / offlinenavigator.onLine, updated on the window online/offline events, with a since timestamp of the last transition
  • Network InformationeffectiveType, downlink, downlinkMax, rtt, saveData, type from navigator.connection (with mozConnection/webkitConnection fallback), updated on its change event
  • Graceful degradation — every Network Information field is undefined where the API is unsupported (Firefox, Safari); online always works and the hook never throws
  • SSR-safe & concurrent-safe — built on useSyncExternalStore; returns a deterministic { online: true } on the server, so there is no hydration mismatch
  • TypeScript-first — full type inference and exported types (including the EffectiveConnectionType and ConnectionType unions)
  • Tiny & tree-shakeable — zero dependencies, published as its own package

Installation

# npm
npm install @usefy/use-network-state

# yarn
yarn add @usefy/use-network-state

# pnpm
pnpm add @usefy/use-network-state

Requires React 18 or 19 (peerDependencies: "react": "^18.0.0 || ^19.0.0").

Quick Start

import { useNetworkState } from "@usefy/use-network-state";

function ConnectivityBanner() {
  const { online } = useNetworkState();
  if (online) return null;
  return <div role="alert">You are offline.</div>;
}

Adapt behaviour to connection quality:

import { useNetworkState } from "@usefy/use-network-state";

function Hero() {
  const { effectiveType, saveData } = useNetworkState();
  const lowData = saveData || effectiveType === "slow-2g" || effectiveType === "2g";
  return lowData ? <LowResImage /> : <HighResImage />;
}

API

useNetworkState(): NetworkState

Takes no arguments and returns the current network state. The object identity is stable between updates that don't change any observed value (safe as an effect dependency).

NetworkState

| Field | Type | Description | | --------------- | ------------------------------------------------- | ----------------------------------------------------------------------------------------------- | | online | boolean | From navigator.onLine. Defaults to true on the server / without a navigator. | | since | Date \| undefined | Timestamp of the last online/offline transition. undefined until the first transition occurs. | | downlink | number \| undefined | Estimated downlink speed in Mb/s. | | downlinkMax | number \| undefined | Maximum downlink speed of the underlying technology, in Mb/s. | | effectiveType | "slow-2g" \| "2g" \| "3g" \| "4g" \| undefined | Effective connection type. | | rtt | number \| undefined | Estimated effective round-trip time in ms. | | saveData | boolean \| undefined | Whether the user has requested reduced data usage ("Data Saver"). | | type | ConnectionType \| undefined | Physical connection type ("wifi", "cellular", "ethernet", …). |

All fields except online are undefined when the Network Information API is unsupported.

Exported types & helpers

import {
  useNetworkState,
  getNetworkState,               // read a one-off snapshot outside React
  getConnection,                 // resolve navigator.connection (+ vendor prefixes)
  isNavigatorAvailable,          // SSR guard
  isNetworkInformationSupported, // feature detection
  areNetworkStatesEqual,         // field-by-field comparison
  SERVER_NETWORK_STATE,          // the inert server snapshot ({ online: true })
  type NetworkState,
  type UseNetworkStateReturn,
  type EffectiveConnectionType,
  type ConnectionType,
  type NetworkInformationLike,
} from "@usefy/use-network-state";

Browser Support

  • Online/offline (online, since) works everywhere.
  • Network Information fields require a Chromium-based browser (Chrome, Edge, Opera, most Android browsers). Firefox and Safari do not implement the API, so those fields are undefined — always treat them as optional.

Testing

📊 View Detailed Coverage Report (GitHub Pages) — 34 tests, 95% statement coverage.

License

MIT © mirunamu

This package is part of the usefy monorepo.