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
-
Make sure you have
@ridgehkr/useful
installed in your application. If not, you can get started by following the installation instructions. -
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:
Property | Type | Description |
---|---|---|
level | number | The battery charge level ranging from 0 to 1 |
charging | boolean | whether the device is currently charging (true ) or not (false ). |