Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-condition

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-condition

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

  • 3.0.3
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
7
decreased by-58.82%
Maintainers
1
Weekly downloads
 
Created
Source

React Condition

Build Status

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

Installation

$ yarn add react-condition
$ 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.

<If test={someCondition}>
  This will only be shown if someCondition is truthy.
  <ElseIf test={otherCondition}>
    This will only be shown if someCondition is falsey
    and otherCondition is truthy.
    <Else>
      This will only be shown if both someCondition and
      otherCondition are both falsey.
    </Else>
  </ElseIf>
  <Else>
    This will be shown if someCondition is falsey.
    <If test={finalCondition}>
      This will be shown if someCondition is falsey
      and finalCondition is truthy.
    </If>
  </Else>
</If>

Alternatively, you can provide then and else props.

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

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>.

<Switch expression={"blue"}>
    <Case value={"red"}>
        red
    </Case>
    <Case value={"green"}>
        green
    </Case>
    <Case value={"blue"}>
        blue
    </Case>
</Switch>
<Switch expression={"hot fucking pink"}>
    <Case value={"red"}>
        red
    </Case>
    <Case value={"green"}>
        green
    </Case>
    <Case value={"blue"}>
        blue
    </Case>
    <Default>
        no color
    </Default>
</Switch>

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

<Switch expression={"hot fucking pink"}>
    <Case value={"red"} then={"red"} />
    <Case value={"red"} then={"green"} />
    <Case value={"red"} then={"blue"} />
    <Default then={"no color"} />
</Switch>

See also

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

FAQs

Package last updated on 07 Dec 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc