Skip to content

useBattery ​

Hook for getting information about battery status

browser
test coverage
Last changed: last month

TIP

This hook uses navigator.getBattery browser api to provide enhanced functionality. Make sure to check for compatibility with different browsers when using this api

Installation ​

Library
CLI
Manual
typescript
import { useBattery } from '@siberiacancode/reactuse';

Usage ​

typescript
const { supported, loading, charging, chargingTime, dischargingTime, level } = useBattery();

Demo ​

Api ​

Returns

UseBatteryStateReturn

Type declaration ​

typescript
export interface BatteryManager extends EventTarget {
  charging: boolean;
  chargingTime: number;
  dischargingTime: number;
  level: number;
}

interface Navigator {
    readonly getBattery: () => Promise<BatteryManager>;
  }

export interface UseBatteryValue {
  /** Is charging battery? */
  charging: boolean;
  /** Time until the battery is fully charged */
  chargingTime: number;
  /** Time until the battery is completely discharged */
  dischargingTime: number;
  /** Battery charge level from 0 to 1 */
  level: number;
  /** Is battery information loading? */
  loading: boolean;
}

export interface UseBatteryStateReturn {
  /** Whether the battery api is supported*/
  supported: boolean;
  /** The use battery value type  */
  value: UseBatteryValue;
}

Source ​

Source • Demo

Contributors ​

D
debabin
debabin
B
babin
babin
H
hywax
hywax
N
Nikita
Nikita
G
Gorilla Dev
Gorilla Dev

Released under the MIT License.