0.0.11 • Published 7 years ago

react-switchcase-condition v0.0.11

Weekly downloads
2
License
MIT
Repository
github
Last release
7 years ago

React Switch Case Condition

Build Status

Declarative way to render components conditionally

Demo

Install

npm install --save react-switchcase-condition
# OR
yarn add react-switchcase-condition

Usage

import {
  Switch,
  Case,
  Default,
} from 'react-switchcase-condition';

const Test1 = ({ text }) => <div>{text}</div>;
const Test2 = ({ text }) => <div>{text}</div>;
const Test3 = ({ text }) => <div>{text}</div>;

const Demo = () => (
  <Switch>
    <Case
      cond={false}
      component={Test1}
      componentProps={{ text: 'This will not be rendered', }}
    />
    <Case
      cond={true}
      component={Test2}
      componentProps={{ text: 'This will be render', }}
    />
    <Default
      component={Test3}
      componentProps={{ text: 'This is the default render' }}
    />
  </Switch>
);

Components

Switch

Wrapper component to declare the Switch block. Its children must contain at least one Case and exact one Default component. The first Case whose cond is true will be rendered. If there is no cond-true Case, Default will be rendered.

Case

Declare a render case

Prop NameTypeRequiredDefaultDescription
condbooltrueCondition to determine render. Render if true and is the first Case in children list
componentReact componenttrueComponent to render if cond is true
componentPropsobjectfalse{}Props passed to component to render if cond is true

Default

Declare the default render case

Prop NameTypeRequiredDefaultDescription
componentReact componenttrueComponent to render if there is no cond-true Case
componentPropsobjectfalse{}Props passed to component to render

License

MIT

0.0.11

7 years ago

0.0.10

7 years ago

0.0.9

7 years ago

0.0.8

7 years ago

0.0.7

7 years ago

0.0.6

7 years ago

0.0.5

7 years ago

0.0.4

7 years ago

0.0.3

7 years ago

0.0.2

7 years ago

0.0.1

7 years ago