Skip to content

Latest commit

 

History

History
86 lines (71 loc) · 2.37 KB

use-battery-level.md

File metadata and controls

86 lines (71 loc) · 2.37 KB

useBatteryLevel

Get or track the battery level or percentage remaining with Battery

releases builds demo

Other hooks   —   Usage   —   Changelog


expo install @use-expo/battery expo-battery

Usage

// full hook
const [batteryLevel, getBatteryLevel] = useBatteryLevel();

// other options
useBatteryLevel({ get: false, listen: false });

Example

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

API

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