Skip to content

Latest commit

 

History

History
92 lines (76 loc) · 2.52 KB

use-battery.md

File metadata and controls

92 lines (76 loc) · 2.52 KB

useBattery

Get the battery level, state and power mode with Battery

releases builds demo

Other hooks   —   Usage   —   Changelog


expo install @use-expo/battery expo-battery

Usage

// full hook
const [battery, getBattery] = useBattery();

// other options
useBattery({ get: false });

Example

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}%`;
}

API

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