fn
useBattery
v0.0.14testeddemoReactive Battery Status API. Tracks the device charging state,
charge level, and the estimated charging/discharging times, keeping them in
sync with the underlying BatteryManager events.
Examples
ts
const { charging, level, chargingTime, dischargingTime } = useBattery();ts
// React to acquisition failures
const { isSupported } = useBattery({ onError: (e) => report(e) });Demo
Loading demo…
Signature
ts
export function useBattery(options: UseBatteryOptions ={ ... }Parameters
| Parameter | Type | Description |
|---|---|---|
options? | UseBatteryOptions | Options |
Returns
UseBatteryReturnReactive support flag, charging state, charging/discharging times, and level| Property | Type | Description |
|---|---|---|
isSupported | Readonly<Ref<boolean>> | Whether the Battery Status API is supported in the current environment. |
charging | Readonly<Ref<boolean>> | Whether the device is currently charging. |
chargingTime | Readonly<Ref<number>> | Seconds remaining until the battery is fully charged, or 0 if already
full / not charging. Infinity while charging time is unknown. |
dischargingTime | Readonly<Ref<number>> | Seconds remaining until the battery is fully discharged, or 0.
Infinity while discharging time is unknown. |
level | Readonly<Ref<number>> | Battery charge level as a number between 0 and 1. |