Skip to content

useNetwork

Reports online state and network information when available.

Reports online state and network information when available.

Import

import { useNetwork } from '@kamod-ch/hooks'

Live demo

Basic demo

Reports online state and network information when available.

Browser-only demo

This demo starts after the page mounts in the browser.
import { useNetwork } from '@kamod-ch/hooks'

export default function UseNetworkDemo() {
  const network = useNetwork()
  return <pre>{JSON.stringify(network, null, 2)}</pre>
}

API

TypeScript signature
export interface NetworkState {
    since?: Date;
    online?: boolean;
    rtt?: number;
    type?: string;
    downlink?: number;
    saveData?: boolean;
    downlinkMax?: number;
    effectiveType?: string;
}
declare function useNetwork(): NetworkState;
export default useNetwork;
/

SSR considerations

This hook touches browser APIs. Render it after mount or guard it with BrowserOnly in SSR and static builds.

Browser compatibility

Requires the corresponding browser API to exist in the current environment.