# @caslin/react

> A util for making using Caslin with React easier.

Latest version **0.9.4** (published 2019-11-18) · MIT license · 0 weekly downloads

## Install

```sh
npm install @caslin/react
pnpm add @caslin/react
yarn add @caslin/react
bun add @caslin/react
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.9.4 |
| Published | 2019-11-18 |
| First published | 2019-07-09 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 38.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 10 |
| Author | wtzeng1 |
| Maintainers | wtzeng |
| Keywords | caslin, react |

## Links

- npm: https://www.npmjs.com/package/@caslin/react
- Repository: https://github.com/wtzeng1/caslin/tree/master/packages/caslin-react
- npm.io page: https://npm.io/package/@caslin/react

## Dependencies (1)

- [prop-types](https://npm.io/package/prop-types.md) ^15.7.2

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 0.9.4 (latest) — 2019-11-18
- 0.9.2 — 2019-11-18
- 0.9.1 — 2019-10-28
- 0.1.1 — 2019-09-26
- 0.1.0 — 2019-09-16
- 0.0.4 — 2019-09-16
- 0.0.3 — 2019-07-11
- 0.0.2 — 2019-07-09

## README

# Caslin React

A tool library for making use Caslin in React easier.

English | [中文](https://github.com/wtzeng1/caslin/blob/master/packages/caslin-react/README-zh-cn.md)

## Installation

```shell
npm install @caslin/feature @caslin/react --save
```

## Getting start

Assume that a `feature.js` file already exists, which exposes the feature instance generated by [FeatureBuilder.define()](https://github.com/wtzeng1/caslin/blob/master/packages/caslin-feature#featurebuilder).

### 1. &lt;Can> component

The attributes is the same as calling style of [@caslin/feature](https://github.com/wtzeng1/caslin/blob/master/packages/caslin-feature#feature). When the checking result is true, the children of the `<Can>` component is rendered, you could pass a `fallback` render prop as fallback element.

```jsx
import { Can } from '@caslin/react';
import feature from './feature';

// 'chidlren' as render function
<Can env="featEnv1" action="create" subject="Article" feature={feature}>
  {() => <button onClick={this.createArticle.bind(this)}>Create Article</button>}
</Can>

// not
<Can not env="featEnv1" action="create" subject="Article" feature={feature}>
  {() => <button onClick={this.createArticle.bind(this)}>Create Article</button>}
</Can>

// passThrough, pass match result as parameter
<Can passThrough env="featEnv1" action="create" subject="Article" feature={feature}>
  {(match) => <button disabled={match} onClick={this.createArticle.bind(this)}>Create Article</button>}
</Can>

// 'children' element
<Can env="featEnv1" action="create" subject="Article" feature={feature}>
  <button onClick={this.createArticle.bind(this)}>Create Article</button>
</Can>

// fallback property will be rendered if feature checking result is false
<Can env="featEnv1" action="create" subject="Article" feature={feature} fallback={<div>You don't have permissioin to create Article.</div>}>
  <button>Create Article</button>
</Can>
```

### 2. &lt;Env> component

In general case, when this component is used, the [feature.setEnv()](https://github.com/wtzeng1/caslin/blob/master/packages/caslin-feature#feature) is called and the current default environment is set, you could also pass a `fallback` render prop as fallback element.

```jsx
import { Env } from '@caslin/react';
import feature from './feature';

<Env is="featEnv1" feature={feature}>
  {() => <div>feature env 1</div>}
</Env>

<Env not="featEnv1" feature={feature}>
  {() => <div>feature env 2</div>}
</Env>

<Env in={['featEnv1', 'featEnv2']} feature={feature}>
  {() => <div>feature env 1</div>}
</Env>

<Env notIn={['featEnv2', 'featEnv3']} feature={feature}>
  {() => <div>feature env 1</div>}
</Env>

// passThrough, pass match result as parameter
<Env passThrough is="featEnv1" feature={feature}>
  {(match) => <div>{match ? 'is' : 'not'} env 1</div>}
</Env>

// fallback property will be rendered if feature checking result is false
<Env is="featEnv1" feature={feature} fallback={<div>Current env is not featureEnv1.</div>}>
  {() => <div>feature env 1</div>}
</Env>
```

### 3. Pass defined "feature" to avoid having to pass it every time

#### createCheckerBoundTo()

Pass `feature` as a parameter to `createCheckerBoundTo()` to get the higher-order component.

```jsx
// featChecker.js
import { createCheckerBoundTo } from '@caslin/react';
import feature from './feature';

export default createCheckerBoundTo(feature); // { Can, Env }
```

You can now use the `Can>` and `<Env>` directly, instead of passing `feature` again:

```jsx
import featUtil from './featChecker';
const { Can, Env } = featUtil;

<Can action="create" subject="Article">
  {() => <button onClick={this.createArticle.bind(this)}>Create Article</button>}
</Can>

<Env is="featEnv1">
  {() => <div>feature env 1</div>}
</Env>
```

#### createContextualChecker()

Pass `Context.Consumer` as a parameter to `createContextualChecker()` to get the higher-order component.

```jsx
// featChecker.js
import React from 'react';
import { createContextualChecker } from '@caslin/react';
import feature from './feature';

export const FeatContext = React.createContext(feature);

export default createContextualChecker(FeatContext.Consumer); // { Can, Env }
```

```jsx
// High hierarchy component
import feature from './feature';
import { FeatContext } from './featChecker';

...
<FeatContext.Provider value={feature}>
  {// ...chidlren}
</FeatContext.Provider>
...
```

```jsx
// Use HOC
import featUtil from './featChecker';
const { Can, Env } = featUtil;

<Can action="create" subject="Article">
  {() => <button onClick={this.createArticle.bind(this)}>Create Article</button>}
</Can>

<Env is="featEnv1">
  {() => <div>feature env 1</div>}
</Env>
```

## License

[MIT License](https://github.com/wtzeng1/caslin/blob/master/LICENSE)

---
_Source: https://npm.io/package/@caslin/react · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
