Get or track the battery level or percentage remaining with Battery
Other hooks — Usage — Changelog
expo install @use-expo/battery expo-battery
// full hook
const [batteryLevel, getBatteryLevel] = useBatteryLevel();
// other options
useBatteryLevel({ get: false, listen: false });
import { useBatteryLevel } from '@use-expo/battery';
import { Text, View } from 'react-native';
function BatteryLevelExample() {
const [level] = useBatteryLevel();
return (
<View>
<Text>Battery level:</Text>
<Text>{percentage(level)}</Text>
</View>
);
}
function percentage(level = 0) {
return `${Math.floor(level * 1000) / 10}%`;
}
function useBatteryLevel(options?: Options): Result;
interface Options {
/** If it should fetch the battery level when mounted, defaults to `true` */
get?: boolean;
/** If it should listen to any change in battery level, defaults to `true` */
listen?: boolean;
}
type Result = [
/** The current battery level */
number | undefined,
/** Callback to fetch the battery level */
() => Promise<void>,
];
with ❤️ byCedric