Summary - Permissions
- React Native course
- Intermediate
- Lesson 111/116
- 5 min
Combine expo-permissions and camera/location permissions to write a permission flow.
Summary - Permissions
In this group you learned the foundation of permissions. A permission is the system approval gate for a sensitive resource. expo-permissions is Expo's permission module; it is requested with askAsync.
Camera permission uses Permissions.CAMERA, location permission uses Permissions.LOCATION, notification permission uses Permissions.NOTIFICATIONS. In each you check status and route the flow: if 'granted' continue, otherwise guide the user. These five concepts together form the permission flow of a real mobile app. To keep user trust, asking at the right moment is essential.
Practice Write the usePermissions hook
Write a usePermissions hook. It takes a permissionType parameter. Hold status with useState('undetermined'). Inside useEffect call Permissions.askAsync(permissionType); in .then call setStatus. Return status.
Starter code
// Write the usePermissions hook
// Take permissionType parameter; hold status with useState; ask permission in useEffect; give back the statusSample solution
import { useState, useEffect } from "react";
import * as Permissions from "expo-permissions";
export function usePermissions(permissionType) {
const [status, setStatus] = useState("undetermined");
useEffect(() => {
Permissions.askAsync(permissionType).then(({ status }) => {
setStatus(status);
});
}, [permissionType]);
return status;
}Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which permission is needed for location data?
Permissions.???- Camera
- Notification
- Location Correct answer
- Microphone
Explanation: Location data needs location permission.
2 Match each permission to its resource
| Term | Meaning |
|---|---|
Permissions.CAMERA |
Camera |
Permissions.LOCATION |
GPS |
Permissions.NOTIFICATIONS |
Push notification |
askAsync |
Asks permission |
3 expo-permissions import
import * as Permissions from '____';
import * as Permissions from 'expo-permissions';
Explanation: expo-permissions is imported as the module.