import { useCallback , useEffect , useState } from 'react' ; import { getBatteryLevelAsync , addBatteryLevelListener } from 'expo-battery' ; see https remarks example export function useBatteryLevel ( options = { } ) { const [ data , setData ] = useState < number > ( ) ; const { get = true , listen = true , } = options ; const getBatteryLevel = useCallback ( ( ) => ( getBatteryLevelAsync ( ) . then ( setData ) ) , [ ] ) ; useEffect ( ( ) => { if ( get ) { getBatteryLevel ( ) ; } if ( listen ) { return addBatteryLevelListener ( state => setData ( state . batteryLevel ) ) . remove ; } } , [ get , getBatteryLevel , listen ] ) ; return [ data , getBatteryLevel ] ; } export interface BatteryLevelOptions { /** 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 ; }