react-native-use-permissions
TypeScript icon, indicating that this package has built-in type declarations

0.1.2 • Public • Published

react-native-use-permissions

This library exposes hooks relating to permissions and notifications.

Install

Install with npm: npm install react-native-use-permissions

Usage

usePermissions(permission)

A hook to keep track of a permission's state.

  • permission: Permission - The permission you want to track. Refer to PERMISSIONS 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);
    }
  }

useNotificationStatus(initialValue)

A hook to keep track of the notification status.

  • initialValue: ResultMap[keyof ResultMap] - Optional, will default to RESULTS.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);
      });
    }
  }

Authors

License

This project is licensed under the MIT License

Dependents (0)

Package Sidebar

Install

npm i react-native-use-permissions

Weekly Downloads

2

Version

0.1.2

License

MIT

Unpacked Size

8.69 kB

Total Files

8

Last publish

Collaborators

  • wahaheha
  • lukebrandonfarrell
  • amogh-jrules
  • chestersim
  • rodriigovieira