DEV Community

Discussion on: React Hook: usePermissions

Collapse
 
dance2die profile image
Sung M. Kim

This looks helpful as React is now in Preview.

And would you add the link to the repo by chance?

You can show the repo using following 👇 syntax

{% github sergiodxa/react-use-permissions %}

, which will display the repo like this.

sergiodxa / react-use-permissions

React hook for Permissions API

react-use-permsissions

React hook for Permissions API

Note: This is using the new React Hooks API Proposal which is subject to change until React 16.7 final.

You'll need to install react, react-dom, etc at ^16.7.0-alpha.0

Install

yarn add react-use-permissions

Usage

import usePermissions from '../src';

const format = hasPermissions => {
  switch (hasPermissions) {
    // User has granted permissions
    case true: {
      return "Permissions granted";
    }
    // User has denied permissions
    case false: {
      return "Permissions denied";
    }
    // User will be prompted for permissions
    case null: {
      return "Asking for permissions";
    }
  }
}

function App() {
  const hasPermissions = usePermissions("geolocation");
  const content = format(hasPermissions);
  return <h1>{content}</h1>;
}