Search code examples
react-nativepermissionslocationexpo

React Native expo-permission deprecated what to use now?


I am using Permissions from the expo-permission Library to get the location coords of the user:

import * as Location from "expo-location";
import * as Permissions from 'expo-permissions';

const granted = await Permissions.askAsync(Permissions.LOCATION);

The above works but keeps giving the warning that expo-permissions is deprecated.

If I use:

import {Location, Permissions } from 'expo';

it says Cannot read property 'askAsync' of undefined.

Does someone know what i should use? I use sdk42

Thx!


Solution

  • As this blog by Brent Vatne says,

    expo-permissions has been deprecated in favor of module-specific permissions methods You should migrate from using Permissions.askAsync and Permissions.getAsync to the permissions methods exported by modules that require the permissions.

    For example: you should replace calls to Permissions.askAsync(Permissions.CAMERA) with Camera.requestPermissionsAsync()

    There shouldn’t be two ways to do an identical thing in a single SDK, and so we picked our preferred approach and are consolidating around it.

    So now, you will have to use Permissions from individual packages

    For Location,

    Firstly, install expo-location

    expo install expo-location
    

    Then you can use it like this

    import * as Location from 'expo-location';
    
    let { status } = await Location.requestForegroundPermissionsAsync();
    if (status !== 'granted') {
      console.log('Permission to access location was denied');
      return;
    }