API Reference
useBatteryStatus

useBatteryStatus

The useBatteryStatus hook provides real-time monitoring of the device's battery level and charging status. This hook utilizes the Battery Status API (opens in a new tab) when available in the browser to retrieve the necessary information.

Usage

  1. Make sure you have @ridgehkr/useful installed in your application. If not, you can get started by following the installation instructions.

  2. Import useBatteryStatus into your component.

import { useBatteryStatus } from '@ridgehkr/useful'

Example

BatteryStatus.tsx
import useBatteryStatus, { BatteryStatus } from '@ridgehkr/useful'
 
function BatteryStatusMonitor() {
  const { level, charging }: BatteryStatus = useBatteryStatus()
 
  return (
    <div>
      <p>Battery Level: {level}</p>
      <p>Charging: {charging ? 'Yes' : 'No'}</p>
    </div>
  )
}

API

Hook Signature

useBatteryStatus(): BatteryStatus

Parameters

useBatteryStatus does not accept any parameters.

Return Value

The hook returns a BatteryStatus object with the following properties:

PropertyTypeDescription
levelnumberThe battery charge level ranging from 0 to 1
chargingbooleanwhether the device is currently charging (true) or not (false).