UNPKG

@unikue/react-condition

Version:

Render components conditionally for react

305 lines (241 loc) 6.76 kB
# @unikue/react-condition [![NPM version](https://img.shields.io/npm/v/@unikue/react-condition.svg?style=flat)](https://npmjs.org/package/@unikue/react-condition) [![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat)](LICENSE.txt) [![NPM downloads](http://img.shields.io/npm/dm/@unikue/react-condition.svg?style=flat)](https://npmjs.org/package/@unikue/react-condition) 🏅 React 条件性渲染组件 👍 ## 特性 ✅ 支持 'If' 条件渲染 ✅ 支持 'If'-'Then' 条件渲染 ✅ 支持 'If'-'Else' 条件渲染 ✅ 支持 'If'-'Then'-Else' 条件渲染 ✅ 支持 'For' 条件渲染 ✅ 支持 'Do' 条件渲染 ✅ 支持 'While' 条件渲染 ✅ 支持 'MapIterator' 条件渲染 ✅ 支持 'SetIterator' 条件渲染 ✅ 支持 'ObjectIterator' 条件渲染 ## 快速开始 您可以在您的 React 项目中使用以下命令来安装本组件库: ```bash $ npm install @unikue/react-condition --save ``` 然后,您可以使用以下命令来导入组件: ```jsx | pure import {If, For, Switch, Do, While} from '@unikue/react-condition'; import {MapIterator, SetIterator, ObjectIterator} from '@unikue/react-condition'; ``` 享受您和 `react-condition` 的编程之旅吧 ✌️ ## 示例 ### If > `If.Then``If.Else` 都有一个 `render` 属性 `() => React.ReactNode`,这样您也可以通过它来返回自定义的渲染内容, 比 React 的 `children` 属性权重要高。 #### `If` 语句 ```jsx | pure import React from 'react'; import {If} from '@unikue/react-condition'; export default () => { const param = true; return ( <If condition={param}> <span>Hello World</span> </If> ); } ``` #### `If`-`Then` 语句 ```jsx | pure import React from 'react'; import {If} from '@unikue/react-condition'; export default () => { const param = 1; return ( <If condition={param}> <If.Then> <span>Hello World</span> </If.Then> </If> ); } ``` #### `If`-`Else` 语句 ```jsx | pure import React from 'react'; import {If} from '@unikue/react-condition'; export default () => { const param = false; return ( <If condition={param}> <span>Hello World</span> <If.Else> <span>Hello Unikue</span> </If.Else> </If> ); } ``` #### `If`-`Then`-`Else` 语句 ```jsx | pure import React from 'react'; import {If} from '@unikue/react-condition'; export default () => { const param = false; return ( <If condition={param}> <If.Then> <span>Hello World</span> </If.Then> <If.Else> <span>Hello Unikue</span> </If.Else> </If> ); } ``` ### `For` 语句 ```jsx | pure import React from 'react'; import {For} from '@unikue/react-condition'; export default () => { return ( <For of={['foo', 'bar']} render={(item, index) => { return ( <span key={index}>Hello, {item}</span> ); }} /> ); } ``` ### `Switch` 语句 > `Switch.Case``Switch.Default` 都有一个 `render` 属性 `() => React.ReactNode`,这样您也可以通过它来返回自定义的渲染内容, 比 React 的 `children` 属性权重要高。 ```jsx | pure import React from 'react'; import {Switch} from '@unikue/react-condition'; export default () => { const username = 'admin'; return ( <Switch> <Switch.Case condition={username.includes('admin')}> <span>admin</span> </Switch.Case> <Switch.Case condition={username.includes('guest')}> <span>guest</span> </Switch.Case> <Switch.Default> <span>root</span> </Switch.Default> </Switch> ); } ``` ### `Do` 语句 ```jsx | pure import React from 'react'; import {Do} from '@unikue/react-condition'; export default () => { let param = 0; return ( <Do condition={() => { return param < 2; }} render={(index) => { param++; return ( <span key={index}>Hello, {index}</span> ); }} /> ); } ``` ### `While` 语句 ```jsx | pure import React from 'react'; import {While} from '@unikue/react-condition'; export default () => { let param = 0; return ( <While condition={() => { return param++ < 2; }} render={(index) => { return ( <span key={index}>Hello, {index}</span> ); }} /> ); } ``` ### `MapIterator` 语句 ```jsx | pure import React from 'react'; import {MapIterator} from '@unikue/react-condition'; export default () => { const map = new Map([ ['foo', 'bar'], ['hello', 'world'], ]); return ( <MapIterator of={map} render={(value, key, index) => { return ( <span key={index}>Hooray, {key}-{value}</span> ); }} /> ); } ``` ### `SetIterator` 语句 ```jsx | pure import React from 'react'; import {SetIterator} from '@unikue/react-condition'; export default () => { const set = new Set<string>([ 'foo-bar', 'hello-world', ]); return ( <SetIterator of={set} render={(item, index) => { return ( <span key={index}>Hooray, {item}</span> ); }} /> ); } ``` ### `ObjectIterator` 语句 ```jsx | pure import React from 'react'; import {ObjectIterator} from '@unikue/react-condition'; export default () => { const param = { 'foo': 'bar', 'hello': 'world', }; return ( <ObjectIterator of={param} render={(value, key, index) => { return ( <span key={index}>Hooray, {key}-{value}</span> ); }} /> ); } ``` ## 授权 本组件库授权基于 [MIT License](https://mit-license.org/) 协议 ## 版权 北京攸科网络科技有限公司 ## 网站 - Unikue: [https://unikue.cn](https://unikue.cn)