Skip to content

useBatteryAware – Battery Status Hook for React

Overview

Exposes device battery status via the Battery Status API. Reacts to real-time chargingchange and levelchange events. SSR-safe with optimistic defaults (charging, 100%).

Usage

import { useBatteryAware } from "react-visibility-hooks";
function BatteryIndicator() {
const { charging, level, isLowBattery, isSupported } = useBatteryAware(0.15);
if (!isSupported) return null;
return (
<div>
<p>
🔋 {Math.round(level * 100)}%{charging ? " ⚡ Charging" : ""}
</p>
{isLowBattery && <p>⚠️ Low battery — reducing activity</p>}
</div>
);
}

Reduce polling on low battery

import { useBatteryAware, useSmartPolling } from "react-visibility-hooks";
function SmartDashboard() {
const { isLowBattery } = useBatteryAware();
const { data } = useSmartPolling(fetchData, {
interval: isLowBattery ? 30_000 : 5_000,
});
return <div>{/* render data */}</div>;
}

API

Parameters

ParameterTypeDefaultDescription
lowThresholdnumber0.15Level (0–1) below which isLowBattery is true

Returns (BatteryState)

PropertyTypeDescription
chargingbooleantrue when the device is plugged in
levelnumberBattery level between 0 and 1
isLowBatterybooleantrue when not charging and level is below threshold
isSupportedbooleantrue when the Battery Status API is available