can-it.github.io

@can-it/react

@can-it/react is a React library designed to simplify the implementation of authorization in your application. The library provides a straightforward way to integrate complex authorization scenarios into your app within minutes using the CanItProvider. npm version

Examples

👉 Source code

Features

Installation

To install @can-it/react, you can use npm, yarn, or pnpm. Run one of the following commands in your project directory:

npm install @can-it/react
yarn add @can-it/react
pnpm add @can-it/react

Usage

  1. Import the CanItProvider into your React application’s component:

     import { CanItProvider } from '@can-it/react';
    
     export function HomeComponent() {
       return (
         <CanItProvider
           comparators=
           {/* You can pass it later by using the usePolicyStore hook */}
           policy=>
    
           <ProductComponent />
         </CanItProvider>
       );
     }
    
    • By default, the CanItProvider will use the compare action and RI in an exact way. If you want to compare it in another way, you can pass it via CanItProviderProps comparators. You can replace it with our other can-it-operators here.
    • You also passed the policy state in its props, but it’s not required. You can pass it later by using the usePolicyStore hook, which is mentioned below.

Then: