Security News
pnpm 10.0.0 Blocks Lifecycle Scripts by Default
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
@caslin/react
Advanced tools
A tool library for making use Caslin in React easier.
English | 中文
npm install @caslin/feature @caslin/react --save
Assume that a feature.js
file already exists, which exposes the feature instance generated by FeatureBuilder.define().
The attributes is the same as calling style of @caslin/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.
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>
In general case, when this component is used, the feature.setEnv() is called and the current default environment is set, you could also pass a fallback
render prop as fallback element.
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>
Pass feature
as a parameter to createCheckerBoundTo()
to get the higher-order component.
// 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:
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>
Pass Context.Consumer
as a parameter to createContextualChecker()
to get the higher-order component.
// 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 }
// High hierarchy component
import feature from './feature';
import { FeatContext } from './featChecker';
...
<FeatContext.Provider value={feature}>
{// ...chidlren}
</FeatContext.Provider>
...
// 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>
FAQs
A util for making using Caslin with React easier.
We found that @caslin/react demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
Product
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.
Research
Security News
Socket researchers have discovered multiple malicious npm packages targeting Solana private keys, abusing Gmail to exfiltrate the data and drain Solana wallets.