useNetwork
A hook that tracks the state of network connection.
Examples
Default usage
Network information:
{
"online": true,
"rtt": 0,
"saveData": false,
"downlink": 10,
"effectiveType": "4g"
}Basic usage
Return network status
API
interface NetworkState { online?: boolean; since?: Date; rtt?: number; type?: string; downlink?: number; saveData?: boolean; downlinkMax?: number; effectiveType?: string;}const result: NetworkState = useNetwork();
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