Get the battery level, state and power mode with Battery
Other hooks — Usage — Changelog
expo install @use-expo/battery expo-battery
// full hook
const [battery, getBattery] = useBattery();
// other options
useBattery({ get: false });
import { useBattery } from '@use-expo/battery';
import { Text, View } from 'react-native';
function BatteryExample() {
const [battery] = useBattery();
return (
<View>
<Text>Battery:</Text>
{!!battery && (
<View>
<Text>level: {percentage(battery.batteryLevel)}</Text>
<Text>state: {battery.batteryState}</Text>
<Text>low power: {battery.lowPowerMode ? 'enabled' : 'disabled'}</Text>
</View>
)}
</View>
);
}
function percentage(level = 0) {
return `${Math.floor(level * 1000) / 10}%`;
}
import { PowerState } from 'expo-battery';
function useBattery(options?: Options): Result;
interface Options {
/** If it should fetch the battery power state when mounted, defaults to `true` */
get?: boolean;
}
type Result = [
/** The current power state */
PowerState | undefined,
/** Callback to manually get the power state data */
() => Promise<void>,
];
with ❤️ byCedric