use-network
shadcn-hooks/use-networkFreeHook
A hook to get the network state
Install it
npx shadcn@latest add https://shadcn-hooks.com/r/use-network.jsonSource
1import { useCallback, useRef, useSyncExternalStore } from 'react'23export interface NetworkState {4 since?: Date5 online?: boolean6 rtt?: number7 type?: string8 downlink?: number9 saveData?: boolean10 downlinkMax?: number11 effectiveType?: string12}1314function getConnection() {15 const nav = navigator as any16 if (typeof nav !== 'object') {17 return null18 }19 return nav.connection || nav.mozConnection || nav.webkitConnection20}2122function getConnectionProperty(): NetworkState {23 const c = getConnection()24 if (!c) {25 return {}26 }27 return {28 rtt: c.rtt,29 type: c.type,30 saveData: c.saveData,31 downlink: c.downlink,32 downlinkMax: c.downlinkMax,33 effectiveType: c.effectiveType,34 }35}3637function serverSnapshot() {38 return {39 since: undefined,40 online: true,41 ...getConnectionProperty(),42 }43}4445function getInitialOnlineState(): boolean {46 if (typeof navigator !== 'object') {47 return true48 }49 return navigator.onLine50}5152export function useNetwork() {53 const networkState = useRef<NetworkState>({54 since: undefined,55 online: getInitialOnlineState(),56 ...getConnectionProperty(),57 })5859 const subscribe = useCallback((onStoreChange: () => void) => {60 const onOnline = () => {61 networkState.current = {62 ...networkState.current,63 since: new Date(),64 online: true,65 ...getConnectionProperty(),66 }67 onStoreChange()68 }6970 const onOffline = () => {71 networkState.current = {72 ...networkState.current,73 since: new Date(),74 online: false,75 ...getConnectionProperty(),76 }77 onStoreChange()78 }7980 const onConnectionChange = () => {81 networkState.current = {82 ...networkState.current,83 since: new Date(),84 ...getConnectionProperty(),85 }86 onStoreChange()87 }8889 window.addEventListener('online', onOnline)90 window.addEventListener('offline', onOffline)9192 const connection = getConnection()93 connection?.addEventListener('change', onConnectionChange)9495 return () => {96 window.removeEventListener('online', onOnline)97 window.removeEventListener('offline', onOffline)98 connection?.removeEventListener('change', onConnectionChange)99 }100 }, [])101102 const snapshot = useCallback(() => networkState.current, [])103104 return useSyncExternalStore(subscribe, snapshot, serverSnapshot)105}
What it pulls in
Other registry items
Files it writes
More from shadcn-hooks
All 58 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| use-active-elementuse-active-element | shadcn-hooks | Hooks | Free | no deps | |
| use-batteryuse-battery | shadcn-hooks | Hooks | Free | no deps | |
| use-booleanuse-boolean | shadcn-hooks | Hooks | Free | no deps | |
| use-click-any-whereuse-click-any-where | shadcn-hooks | Hooks | Free | no deps | |
| use-click-awayuse-click-away | shadcn-hooks | Hooks | Free | no deps | |
| use-clipboarduse-clipboard | shadcn-hooks | Hooks | Free | no deps | |
| use-controllable-valueuse-controllable-value | shadcn-hooks | Hooks | Free | 1 dep | |
| use-counteruse-counter | shadcn-hooks | Hooks | Free | no deps |
