This library exposes hooks relating to permissions and notifications.
Install with npm:
npm install react-native-use-permissions
A hook to keep track of a permission's state.
permission: Permission
- The permission you want to track. Refer toPERMISSIONS
from'react-native-permissions'
for the list of supported permissions.
Example:
import { usePermissions } from 'react-native-use-permissions';
import { RESULTS, PERMISSIONS } from 'react-native-permissions';
...
/** State */
const [cameraPermissionState, cameraPermissionRequest] = usePermission(PERMISSIONS.ANDROID.CAMERA);
return (
<Button title="Request Permission" onPress={onRequestPermission} />
)
/**
* Asks for permission if we don't have it yet
*/
function onRequestPermission() {
// Optional
const rationale = {
title: 'Requesting for Camera Permission'
message: 'We need to access your camera to scan QR codes.'
buttonPositive: 'Okay'
}
if (cameraPermissionState === RESULTS.DENIED) {
request(rationale);
}
}
A hook to keep track of the notification status.
initialValue: ResultMap[keyof ResultMap]
- Optional, will default toRESULTS.BLOCKED
Example:
import { useNotificationStatus } from 'react-native-use-permissions';
import { RESULTS } from 'react-native-permissions';
....
/** State */
const [notificationStatus, setNotificationStatus] = useNotificationStatus();
return (
<Button title="Enable Notifications" onPress={onEnableNotifications} />
)
/**
* Asks for notification if we haven't asked for it yet
*/
function onEnableNotifications() {
if (notificationStatus === RESULTS.DENIED) {
requestNotifications(['alert', 'badge']).then((value) => {
setNotificationStatus(value.status);
});
}
}
- Luke Brandon Farrell - Author
This project is licensed under the MIT License