项目作者: johipsum

项目描述 :
small helper component for conditional rendering
高级语言: JavaScript
项目地址: git://github.com/johipsum/if-else-react.git
创建时间: 2020-10-08T14:20:04Z
项目社区:https://github.com/johipsum/if-else-react

开源协议:MIT License

下载


if-else-react · npm

Small helper component for conditional rendering.

This

  1. render() {
  2. const isLoggedIn = this.state.isLoggedIn;
  3. return (
  4. <div>
  5. {isLoggedIn ? (
  6. <span>
  7. <Button onClick={this.handdleSettingsClick}>Account Settings</Button>
  8. <Button onClick={this.handleLogoutClick}>Logout</Button>
  9. </span>
  10. ) : (
  11. <span>
  12. <Button onClick={this.handleLoginClick}>Login</Button>
  13. <Button onClick={this.handleSignUpClick}>Sign Up</Button>
  14. </span>
  15. )}
  16. </div>
  17. );
  18. }

becomes this ✨

  1. render() {
  2. const isLoggedIn = this.state.isLoggedIn;
  3. return (
  4. <div>
  5. <If condition={isLoggedIn}>
  6. <Button onClick={this.handdleSettingsClick}>Account Settings</Button>
  7. <Button onClick={this.handleLogoutClick}>Logout</Button>
  8. <Else ></Else>
  9. <Button onClick={this.handleLoginClick}>Login</Button>
  10. <Button onClick={this.handleSignUpClick}>Sign Up</Button>
  11. </If>
  12. </div>
  13. );
  14. }

Getting Started

Install via npm:

  1. $ npm install if-else-react --save

and then just import it in your app

  1. import If, { Else } from 'if-else-react';

and use it like shown in the example above. done 🎉

The If Component

The If Component is only rendering whats inside if the condition prop is true.
If there is a Else component as a direct children then all children components after the Else will be rendered if the condition is false.

Props
Prop Type Default Required?
condition boolean undefined no

The Else Component

The Else Component renders and does nothing by itself. But when used inside of a Ifthen it seperates what is rendered based on the condition passed into the If.

Props
Prop Type Default Required?
- - - -