raddix

useBattery

Tracking the battery status of the device.

Installation

Install the custom hook from your command line.

npm i @raddix/use-battery

Usage

import { useBattery } from '@raddix/use-battery'
const App = () => {
const { level, charging } = useBattery()
return (
<div>
<p>Battery level:{level && level * 100}</p>
<p>{charging ? 'Battery charging' : 'Battery not charging'}</p>
</div>
)
}
export default App

API

Return

The hook returns an object with the following properties:

NameDescriptionType
isSupportedIndicates whether the Battery Status API is supported in the user’s browser.boolean
isLoadingIndicates if the battery information is still loading.boolean
levelRepresents the level of the system’s battery. 0.0 means that the system’s battery is completely discharged, and 1.0 means the battery is completely charged.number
chargingIndicates whether the system’s battery is charging. true means the battery is charging, false means it’s not.boolean
chargingTimeRepresents the time remaining in seconds until the system’s battery is fully charged.number
dischargingTimeRepresents the time remaining in seconds until the system’s battery is completely discharged and the system is about to be suspended.number