React If Component
Render React components conditionally.
This component helps you turn this
{ return <div> <Header /> this </Footer> </div> ;} { return thispropsage >= thispropsdrinkingAge ? <span class="ok">Have a beer thispropsname!</span> : <span class="not-ok">Sorry thispropsname you are not old enough</span>;}
into this
{ return <div> <Header /> <If when= thispropsage >= thispropsdrinkingAge > <Then><span class="ok">Have a beer thispropsname!</span></Then> <Else><span class="not-ok">Sorry thispropsname you are not old enough</span></Else> </If> </Footer> </div> ;}
Caveats
With this approach to conditional elements, children of either branch will always be evaluated no matter what. This can be an issue eg. if you're testing an object for nullity, and then try to access one of its property inside of the Then
branch. See this StackOverflow discussion as well as issue #1 for more informations.
Install
NPM:
npm install react-if-component
Example
// Browserify:var If = ; // Otherwisevar If = ReactIf;var Stage1 = React; var Stage2 = React; var Beer = React; React;
API
<If />
Property | Type |
---|---|
when |
Boolean |
If when
evaluates to true
, renders the children block will be rendered, otherwise renders the null block.
License
React If Component is released under the MIT license.