Code Monkey home page Code Monkey logo

react-hook-use-permissions's Introduction

react-hook-use-permissions

platforms npm npm github issues github closed issues



Table of contents

  1. Usage
    1. Installation
    2. Hooks
    3. Component
  2. Credits

Usage

Installation

Using npm:

$ npm install --save react-hook-use-permissions

Using yarn:

$ yarn add react-hook-use-permissions

Hooks

hasAny

This method checks whether one of the permissions passed as a function parameter exists in the permissions array passed in the hook

hasAll

This method checks whether all permissions passed as a function parameter exist in the permissions array passed in the hook

doesNotHaveAny

This method checks that one of the permissions passed as a function parameter do not exist in the permissions array passed in the hook

doesNotHaveAll

This method checks that all permissions passed as a function parameter do not exist in the permissions array passed in the hook

*In all methods, you can use a string with the permissions separated by | (pipe) or a array

import { usePermissions } from "react-hook-use-permissions";

export default function App() {
  /** Here you can use any way to instantiate permissions, for example through states using redux **/
  const permissions = ["store", "edit"];
  const { hasAny, hasAll, doesNotHaveAny, doesNotHaveAll } = usePermissions<string[]>(permissions);

  return (
    <>
      {hasAny("store|edit|remove") ? (
        <div>Have any of the permissions</div>
      ) : (
        <div>Does not have any of the permissions</div>
      )}

      {hasAll(["store", "edit", "remove"]) ? (
        <div>Has all of the permissions</div>
      ) : (
        <div>Does not have all of the permissions</div>
      )}

      {doesNotHaveAny("store|edit|remove") ? (
        <div>Does not have any of the permissions</div>
      ) : (
        <div>Has any of the permissions</div>
      )}

      {doesNotHaveAll("store|edit") ? (
        <div>Does not have all of the permissions</div>
      ) : (
        <div>Has any or all of the permissions</div>
      )}
    </>
  );
}

Usage With Redux

To use with redux the only thing that will be different is the instantiation of the hook, you will use the hook usePermissionsWithRedux, and you will have to pass as a parameter to the hook a function to be used in the redux selector, state => state.user.permissions for example.

import { usePermissionsWithRedux } from "react-hook-use-permissions";
import {RootState, UserState} from "./redux-types";

export default function App() {
  const { hasAny, hasAll, doesNotHaveAny, doesNotHaveAll } = usePermissionsWithRedux<RootState, UserState>((state) => state.user.permissions);

  return (
    <>
      {hasAny("store|edit|remove") ? (
        <div>Have any of the permissions</div>
      ) : (
        <div>Does not have any of the permissions</div>
      )}

      {hasAll(["store", "edit", "remove"]) ? (
        <div>Has all of the permissions</div>
      ) : (
        <div>Does not have all of the permissions</div>
      )}

      {doesNotHaveAny("store|edit|remove") ? (
        <div>Does not have any of the permissions</div>
      ) : (
        <div>Has any of the permissions</div>
      )}

      {doesNotHaveAll("store|edit") ? (
        <div>Does not have all of the permissions</div>
      ) : (
        <div>Has any or all of the permissions</div>
      )}
    </>
  );
}

Component

Permission

This is a component that uses the usePermissions or usePermissionsWithRedux hook inside itself

import { Permission } from "react-hook-use-permissions";

export default function App() {
  /** Here you can use any way to instantiate permissions, for example through states using redux **/
  const permissions = ["store", "edit"];

  return (
    <Permission
      permissions={permissions}
      doesNotHaveAny="store|edit"
      /**
       * You can also pass permissions on an array like this ['store', 'edit'] to doesNotHaveAny prop
       **/

      /**
       * You can also pass any method described above to verify permissions
       **/
    >
      {/**Put here the content you want**/}
    </Permission>
  );
}

Usage With Redux

import { Permission } from "react-hook-use-permissions";
import {RootState, UserState} from "./redux-types";

export default function App() {
  return (
    <Permission<RootState, UserState>
      selector={(state) => state.user.permissions}
      hasAny="store|edit"
      /**
       * You can also pass permissions on an array like this ['store', 'edit'] to hasAny prop
       **/

      /**
       * You can also pass any method described above to verify permissions
       **/
    >
      {/**Put here the content you want**/}
    </Permission>
  );
}

Props

Prop Type Description
children ReactNode React Node(s) to render.
permissions ?string[] Permissions that will be used for verification inside of the component. Required if selector prop are not present
hasAny ?string|string[] Permissions to be checked by method hasAny present in usePermission or usePermissionWithRedux. If you pass permissions as a string, they must be separated by | (pipe)
hasAll ?string|string[] Permissions to be checked by method hasAll present in usePermission or usePermissionWithRedux. If you pass permissions as a string, they must be separated by | (pipe)
doesNotHaveAny ?string|string[] Permissions to be checked by method doesNotHaveAny present in usePermission or usePermissionWithRedux. If you pass permissions as a string, they must be separated by | (pipe)
doesNotHaveAll ?string|string[] Permissions to be checked by method doesNotHaveAll present in usePermission or usePermissionWithRedux. If you pass permissions as a string, they must be separated by | (pipe)
selector (state: DefaultRootState) => string[] Selector used by the redux to get permissions. Required if permissions prop are not present

If you do not pass any permissions on permissions property, the component will render the content as if the user has permission.

Credits

Written by Julio Cavallari (julio-cavallari).

react-hook-use-permissions's People

Contributors

julio-cavallari avatar luisrabock avatar

Stargazers

Wildes avatar  avatar Samuel avatar  avatar  avatar A.O avatar Dennis Mwema avatar Thomas Macfarlaine avatar Adam Radestock avatar

Watchers

 avatar

react-hook-use-permissions's Issues

2.0.0 package on npm is missing files.

When I install the 2.0.0 version with npm I get a package that is missing the index.js files and other transpiled files. Therefore the package cannot be used in a Javascript project.

Type error

skipLibcheck: true is dose not working also change target to es5 it's dose not work when git pre-commit

My tsconfig.ts

"target": "es6",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"allowSyntheticDefaultImports": true

Screen Shot 2564-02-10 at 14 41 59

Update for React 18

When trying to install this package in a React 18 project or updating a project to React 18, the dependency set in package.json is not met. Is is set on:
react@"^16.8.0 || ^17.0.0"

It would be great if this package is updated to also accept React 18 as a correct dependency.

The package does work correctly on React 18 with the limited testing I did with version 1.0.7. This modification is available on our fork available at: https://github.com/the-line/react-hook-use-permissions/tree/1-0-7-react-18

I could not get version 2.0.0 working with npm. I made a issue for this problem: #10

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.