useNetwork
A hook that tracks the state of network connection.
Overview
A hook that tracks the state of network connection.
Installation
Open inpnpm dlx shadcn@latest add https://shadcn-ahooks.vercel.app/r/useNetwork.jsonnpx shadcn@latest add https://shadcn-ahooks.vercel.app/r/useNetwork.jsonyarn shadcn@latest add https://shadcn-ahooks.vercel.app/r/useNetwork.jsonbun shadcn@latest add https://shadcn-ahooks.vercel.app/r/useNetwork.jsonA hook that tracks the state of network connection.
Examples
Default usage
API
interface NetworkState {
online?: boolean;
since?: Date;
rtt?: number;
type?: string;
downlink?: number;
saveData?: boolean;
downlinkMax?: number;
effectiveType?: string;
}
const result: NetworkState = useNetwork();Result
| Property | Description | Type |
|---|---|---|
| online | Whether connected to network | boolean |
| since | online latest update time | Date |
| rtt | The effective round-trip time estimate in milliseconds | number |
| type | The connection type that the user agent is using | bluetooth | cellular | ethernet | none | wifi | wimax | other | unknown |
| downlink | The effective bandwidth estimate in megabits per second, | number |
| downlinkMax | An upper bound on the downlink speed of the first network hop | number |
| saveData | Whether the user agent has set the option to reduce data usage | boolean |
| effectiveType | The effective connection type | slow-2g | 2g | 3g | 4g |
More information refer to MDN NetworkInformation
useMutationObserver
A hook that provides the ability to watch for changes being made to the DOM tree, refer to [MutationObserver](https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver)
usePagination
usePagination is implemented based on useRequest and encapsulates common paging logic. The differences from useRequest are as follows.