项目作者: andrewfluck

项目描述 :
React Condition works with React Hooks as part of the React Velcro Architecture, adapted from @leebyron's react-loops
高级语言: JavaScript
项目地址: git://github.com/andrewfluck/react-condition.git
创建时间: 2019-04-03T01:21:22Z
项目社区:https://github.com/andrewfluck/react-condition

开源协议:

下载


React Condition

Build Status

React Condition works with React Hooks as part of @leebyron‘s React Velcro architecture

Installation

  1. $ yarn add react-condition
  1. $ npm i react-condition

If conditions

Use the test prop with <If> and <ElseIf> elements to conditionally include certain elements. When an <If> test is truthy it does not render any <ElseIf> or <Else> children. However when it is falsey it only renders <ElseIf> and <Else> children.

  1. <If test={someCondition}>
  2. This will only be shown if someCondition is truthy.
  3. <ElseIf test={otherCondition}>
  4. This will only be shown if someCondition is falsey
  5. and otherCondition is truthy.
  6. <Else>
  7. This will only be shown if both someCondition and
  8. otherCondition are both falsey.
  9. </Else>
  10. </ElseIf>
  11. <Else>
  12. This will be shown if someCondition is falsey.
  13. <If test={finalCondition}>
  14. This will be shown if someCondition is falsey
  15. and finalCondition is truthy.
  16. </If>
  17. </Else>
  18. </If>

Alternatively, you can provide then and else props.

  1. <If
  2. test={someCondition}
  3. then={"This will only be shown if someCondition is truthy."}
  4. else={"This will be shown if someCondition is falsey."}
  5. ></If>

Switch conditions

Use the expression prop with <Switch> element to conditionally include certain elements. When an <Switch> compares a value from <Case> and the comparison is truthy it only renders the matching child. However, when the comparison is falsey it continues through the children until it finds a match, or falls back to <Default>.

  1. <Switch expression={"blue"}>
  2. <Case value={"red"}>
  3. red
  4. </Case>
  5. <Case value={"green"}>
  6. green
  7. </Case>
  8. <Case value={"blue"}>
  9. blue
  10. </Case>
  11. </Switch>
  1. <Switch expression={"hot fucking pink"}>
  2. <Case value={"red"}>
  3. red
  4. </Case>
  5. <Case value={"green"}>
  6. green
  7. </Case>
  8. <Case value={"blue"}>
  9. blue
  10. </Case>
  11. <Default>
  12. no color
  13. </Default>
  14. </Switch>

Alternatively, you can provide then as props to <Case> or <Default>

  1. <Switch expression={"hot fucking pink"}>
  2. <Case value={"red"} then={"red"} ></Case>
  3. <Case value={"red"} then={"green"} ></Case>
  4. <Case value={"red"} then={"blue"} ></Case>
  5. <Default then={"no color"} ></Default>
  6. </Switch>

See also

@leebyron/react-loops - The father (or mother, idfk) of this library