useNetwork
hookas/use-networkFreeHook
A hook to get the network information
Install it
npx shadcn@latest add https://hookas.letstri.dev/r/use-network.jsonSource
1'use client'23import * as React from 'react'45interface NavigatorWithConnection extends Navigator {6 connection?: NetworkInformation7}89interface NetworkInformation extends EventTarget {10 downlink: number11 downlinkMax?: number12 effectiveType: 'slow-2g' | '2g' | '3g' | '4g'13 rtt: number14 saveData: boolean15 type?: 'bluetooth' | 'cellular' | 'ethernet' | 'none' | 'wifi' | 'wimax' | 'other' | 'unknown'16 addEventListener: (type: 'change', listener: () => void) => void17 removeEventListener: (type: 'change', listener: () => void) => void18}1920export interface NetworkState {21 /**22 * The effective bandwidth estimate in megabits per second23 */24 downlink: number25 /**26 * The maximum downlink speed in megabits per second (experimental)27 */28 downlinkMax: number | undefined29 /**30 * The effective connection type: 'slow-2g', '2g', '3g', or '4g'31 */32 effectiveType: 'slow-2g' | '2g' | '3g' | '4g'33 /**34 * The estimated round-trip time in milliseconds35 */36 rtt: number37 /**38 * Whether the user has enabled data saver mode39 */40 saveData: boolean41 /**42 * The connection type (experimental)43 */44 type: 'bluetooth' | 'cellular' | 'ethernet' | 'none' | 'wifi' | 'wimax' | 'other' | 'unknown' | undefined45 /**46 * Whether the Network Information API is supported47 */48 isSupported: boolean49 /**50 * Whether the connection is considered slow (slow-2g or 2g)51 */52 isConnectionSlow: boolean53 /**54 * Whether the user is currently online55 */56 online: boolean57}5859function getNetworkState(): NetworkState {60 if (typeof window === 'undefined') {61 return {62 downlink: 0,63 downlinkMax: undefined,64 effectiveType: '4g',65 rtt: 0,66 saveData: false,67 type: undefined,68 isSupported: false,69 isConnectionSlow: false,70 online: true,71 }72 }7374 const nav = navigator as NavigatorWithConnection75 const connection = nav.connection7677 if (!connection) {78 return {79 downlink: 0,80 downlinkMax: undefined,81 effectiveType: '4g',82 rtt: 0,83 saveData: false,84 type: undefined,85 isSupported: false,86 isConnectionSlow: false,87 online: window.navigator.onLine,88 }89 }9091 const effectiveType = connection.effectiveType92 const isConnectionSlow = effectiveType === 'slow-2g' || effectiveType === '2g'9394 return {95 downlink: connection.downlink,96 downlinkMax: connection.downlinkMax,97 effectiveType,98 rtt: connection.rtt,99 saveData: connection.saveData,100// … truncated
Files it writes
More from hookas
All 30 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| useAsyncEffectuse-async-effect | hookas | Hooks | Free | no deps | |
| useClickOutsideuse-click-outside | hookas | Hooks | Free | no deps | |
| useDebounceCallbackuse-debounced-callback | hookas | Hooks | Free | no deps | |
| useDebounceMemouse-debounced-memo | hookas | Hooks | Free | no deps | |
| useDebounceStateuse-debounced-state | hookas | Hooks | Free | no deps | |
| useElementSizeuse-element-size | hookas | Hooks | Free | no deps | |
| useFullscreenuse-fullscreen | hookas | Hooks | Free | no deps | |
| useIntervaluse-interval | hookas | Hooks | Free | no deps |
