import { useEffect , useState , useCallback } from 'react' ; import { PowerState , getPowerStateAsync } from 'expo-battery' ; see https remarks example export function useBattery ( options = { } ) { const [ data , setData ] = useState < PowerState > ( ) ; const { get = true } = options ; const getBatteryPowerState = useCallback ( ( ) => ( getPowerStateAsync ( ) . then ( setData ) ) , [ ] ) ; useEffect ( ( ) => { if ( get ) { getBatteryPowerState ( ) ; } } , [ get , getBatteryPowerState ] ) ; return [ data , getBatteryPowerState ] ; } export interface BatteryOptions { /** If it should fetch the battery power state when mounted, defaults to `true` */ get ? : boolean ; }