Get or track the battery (charging) state Battery
Other hooks — Usage — Changelog
expo install @use-expo/battery expo-battery
// full hook
const [batteryState, getBatteryState] = useBatteryState();
// other options
useBatteryState({ get: false, listen: false });
import { useBatteryState } from '@use-expo/battery';
import { Text, View } from 'react-native';
import { BatteryState } from 'expo-battery';
function BatteryStateExample() {
const [batteryState] = useBatteryState();
return (
<View>
<Text>Battery (charging) state:</Text>
<Text>{states[batteryState] || ''}</Text>
</View>
);
}
const states = {
[BatteryState.UNKNOWN]: 'unkown',
[BatteryState.UNPLUGGED]: 'unplugged',
[BatteryState.CHARGING]: 'charging',
[BatteryState.FULL]: 'full',
};
import { BatteryState } from 'expo-battery';
function useBatteryState(options?: Options): Result;
interface Options {
/** If it should fetch the battery state when mounted, defaults to `true` */
get?: boolean;
/** If it should listen to any change in battery state, defaults to `true` */
listen?: boolean;
}
type Result = [
/** The current battery (charging) state */
BatteryState | undefined,
/** Callback to manually get the battery state */
() => Promise<void>,
];
with ❤️ byCedric