Skip to content

Latest commit

 

History

History
92 lines (76 loc) · 2.58 KB

use-battery-state.md

File metadata and controls

92 lines (76 loc) · 2.58 KB

useBatteryState

Get or track the battery (charging) state Battery

releases builds demo

Other hooks   —   Usage   —   Changelog


expo install @use-expo/battery expo-battery

Usage

// full hook
const [batteryState, getBatteryState] = useBatteryState();

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

Example

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',
};

API

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