0.1.12 ā€¢ Published 6 days ago

@yookue/react-condition v0.1.12

Weekly downloads
-
License
MIT
Repository
github
Last release
6 days ago

@yookue/react-condition

NPM version Software License NPM downloads

šŸ… Render components conditionally for React šŸ‘

Features

1ļøāƒ£ Supports 'If' conditions

2ļøāƒ£ Supports 'If'-'Then' conditions

3ļøāƒ£ Supports 'If'-'Else' conditions

4ļøāƒ£ Supports 'If'-'Then'-Else' conditions

šŸ” Supports 'For' conditions

šŸ” Supports 'Do' conditions

šŸ” Supports 'While' conditions

šŸ” Supports 'MapIterator' conditions

šŸ” Supports 'SetIterator' conditions

šŸ” Supports 'ObjectIterator' conditions

Quickstart

You can install this package in your React project as follows:

$ npm install @yookue/react-condition --save

Then, you may import components as follows:

import {If, For, Switch, Do, While, MapIterator, SetIterator, ObjectIterator} from '@yookue/react-condition';

Enjoy your coding journey with react-condition. āœŒļø

Example

If

Both of the If.Then and If.Else have a render property (() => React.ReactNode), thus you can customize the rendering contents instead of the React Children.

The If statement

import React from 'react';
import {If} from '@yookue/react-condition';

export default () => {
    const param = true;
    return (
        <If condition={param}>
            <span>Hello World</span>
        </If>
    );
}

The If-Then statement

import React from 'react';
import {If} from '@yookue/react-condition';

export default () => {
    const param = 1;
    return (
        <If condition={param}>
            <If.Then>
                <span>Hello World</span>
            </If.Then>
        </If>
    );
}

The If-Else statement

import React from 'react';
import {If} from '@yookue/react-condition';

export default () => {
    const param = false;
    return (
        <If condition={param}>
            <span>Hello World</span>
            <If.Else>
                <span>Hello Yookue</span>
            </If.Else>
        </If>
    );
}

The If-Then-Else statement

import React from 'react';
import {If} from '@yookue/react-condition';

export default () => {
    const param = false;
    return (
        <If condition={param}>
            <If.Then>
                <span>Hello World</span>
            </If.Then>
            <If.Else>
                <span>Hello Yookue</span>
            </If.Else>
        </If>
    );
}

For

import React from 'react';
import {For} from '@yookue/react-condition';

export default () => {
    return (
        <For
            of={['foo', 'bar']}
            render={(item, index) => {
                return (
                    <span key={index}>Hello, {item}</span>
                );
            }}
        />
    );
}

Switch

Both of the Switch.Case and Switch.Default have a render property (() => React.ReactNode), thus you can customize the rendering contents instead of the React Children.

import React from 'react';
import {Switch} from '@yookue/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

import React from 'react';
import {Do} from '@yookue/react-condition';

export default () => {
    let param = 0;
    return (
        <Do
            condition={() => {
                return param < 2;
            }}
            render={(index) => {
                param++;
                return (
                    <span key={index}>Hello, {index}</span>
                );
            }}
        />
    );
}

While

import React from 'react';
import {While} from '@yookue/react-condition';

export default () => {
    let param = 0;
    return (
        <While
            condition={() => {
                return param++ < 2;
            }}
            render={(index) => {
                return (
                    <span key={index}>Hello, {index}</span>
                );
            }}
        />
    );
}

MapIterator

import React from 'react';
import {MapIterator} from '@yookue/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

import React from 'react';
import {SetIterator} from '@yookue/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

import React from 'react';
import {ObjectIterator} from '@yookue/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>
                );
            }}
        />
    );
}

License

This project is under the MIT License.

Website

0.1.12

6 days ago

0.1.10

7 days ago

0.1.11

7 days ago

0.1.9

23 days ago

0.1.8

4 months ago

0.1.7

4 months ago

0.1.6

6 months ago

0.1.5

7 months ago

0.1.4

7 months ago

0.1.3

7 months ago

0.1.2

8 months ago

0.1.1

8 months ago

0.1.0

8 months ago