useNetwork

用于追踪网络状态的 React Hook。

演示

源码
Is online:
true
Effective type:
undefined
RTT:
undefined
Downlink:
undefined

用法

请查看 API。

源码

点击下方链接跳转 GitHub 查看源代码。

API

const network = useNetwork()

返回值

export type UseNetworkReturns = {
  /**
   * 浏览器是否支持网络信息 API
   */
  isSupported: boolean
  /**
   * 用户当前是否已连接。
   */
  isOnline: boolean
  /**
   * 用户上次连接以来的时间。
   */
  offlineAt: number | undefined
  /**
   * 此时,如果用户处于离线状态并重新连接。
   */
  onlineAt: number | undefined
  /**
   * 以 Mbps 计的下载速度。
   */
  downlink: number | undefined
  /**
   * 可达到的最大下载速度,以 Mbps 计。
   */
  downlinkMax: number | undefined
  /**
   * 检测到的有效速度类型。
   */
  effectiveType: NetworkEffectiveType | undefined
  /**
   * 当前连接的估计有效往返时间。
   */
  rtt: number | undefined
  /**
   * 用户是否激活了数据节省模式。
   */
  saveData: boolean | undefined
  /**
   * 检测到的连接/网络类型。
   */
  type: NetworkType
}