Skip to main content

useNetwork

Tracks the state of browser's network connection.

As of the standard it is not guaranteed that browser connected to the Internet, it only guarantees the network connection

Usage

Live Editor

function Demo() {
  const state = useNetwork();

  return <pre>{JSON.stringify(state, null, 2)}</pre>;
};

Result
Loading...

API

useNetwork

Returns

IUseNetworkState

Arguments

IUseNetworkState

PropertyDescriptionTypeDefaultValue
onlineWhether browser connected to the network or not.boolean | undefined (Required)-
previousPrevious value of online property. Helps to identify if browserjust connected or lost connection.boolean | undefined (Required)-
sinceThe object pointing to the moment when state change occurred.Date | undefined (Required)-
downlinkEffective bandwidth estimate in megabits per second, rounded to thenearest multiple of 25 kilobits per seconds.INetworkInformation["downlink"] | undefined (Required)-
downlinkMaxMaximum downlink speed, in megabits per second (Mbps), for theunderlying connection technologyINetworkInformation["downlinkMax"] | undefined (Required)-
effectiveTypeEffective type of the connection meaning one of 'slow-2g', '2g', '3g', or '4g'.This value is determined using a combination of recently observed round-trip timeand downlink values.INetworkInformation["effectiveType"] | undefined (Required)-
rttEstimated effective round-trip time of the current connection, roundedto the nearest multiple of 25 millisecondsINetworkInformation["rtt"] | undefined (Required)-
saveData if the user has set a reduced data usage option on the user agent.INetworkInformation["saveData"] | undefined (Required)-
typeThe type of connection a device is using to communicate with the network.It will be one of the following values:- bluetooth- cellular- ethernet- none- wifi- wimax- other- unknownINetworkInformation["type"] | undefined (Required)-

INetworkInformation

export interface INetworkInformation extends EventTarget {
readonly downlink: number;
readonly downlinkMax: number;
readonly effectiveType: "slow-2g" | "2g" | "3g" | "4g";
readonly rtt: number;
readonly saveData: boolean;
readonly type: "bluetooth" | "cellular" | "ethernet" | "none" | "wifi" | "wimax" | "other" | "unknown";
onChange: (event: Event) => void;
}