Last active
December 1, 2019 04:29
-
-
Save jseed/d33a83ff5af16508f5e162ecfd184dca to your computer and use it in GitHub Desktop.
react-native context camera permissions hook
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// PermissionsContext.js | |
import React, { createContext, useState } from 'react'; | |
const PermissionsContext = createContext(); | |
function PermissionsProvider({ children }) { | |
const [hasCameraPermissions, setHasCameraPermissions] = useState(null); | |
return ( | |
<PermissionsContext.Provider value={[hasCameraPermissions, setHasCameraPermissions]}> | |
{children} | |
</PermissionsContext.Provider> | |
); | |
} | |
export { PermissionsProvider, PermissionsContext }; | |
// useCameraPermissions.js - custom hook | |
import { useContext, useEffect } from 'react'; | |
import * as Permissions from 'expo-permissions'; | |
import { PermissionsContext } from '../context/PermissionsContext'; | |
export default function useCameraPermissions() { | |
const [hasCameraPermissions, setHasCameraPermissions] = useContext(PermissionsContext); | |
useEffect(() => { | |
Permissions.askAsync(Permissions.CAMERA).then(({ status }) => | |
setHasCameraPermissions(status === 'granted'), | |
); | |
}, []); | |
return hasCameraPermissions; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment