import { useCallback , useEffect , useState } from 'react' ; import { PermissionType , PermissionResponse , askAsync , getAsync } from 'expo-permissions' ; see https example export function usePermissions ( type , options = { } ) { const [ data , setData ] = useState < PermissionResponse > ( ) ; const types = Array . isArray ( type ) ? type : [ type ] ; const { ask = false , get = true , } = options ; const askPermissions = useCallback ( ( ) => ( askAsync ( ... types ) . then ( setData ) ) , [ type ] ) ; const getPermissions = useCallback ( ( ) => ( getAsync ( ... types ) . then ( setData ) ) , [ type ] ) ; useEffect ( ( ) => { if ( ask ) { askPermissions ( ) ; } if ( ! ask && get ) { getPermissions ( ) ; } } , [ ask , askPermissions , get , getPermissions ] ) ; return [ data , askPermissions , getPermissions ] ; } export interface PermissionsOptions { /** If it should ask the permissions when mounted, defaults to `false` */ ask ? : boolean ; /** If it should fetch information about the permissions when mounted, defaults to `true` */ get ? : boolean ; }