Convenient way to render React components
npm install render-ifelseAn extension to render-if
A tiny, yet conveniently curried way to render conditional React components. Works great with both React and React Native.
``js`
renderIfElse(predicate)(elementOnTrue[,elementOnFalse])
renderIfElse is a curried function that takes a predicate and returns a function accepting two elements, first of them will only be returned if the predicate is satisfied, else second is returned.renderIfElse
The function returned by will also accept parameterless functions which will be invoked similarly, if the predicate is satisfied, first argument is invoked, else second is invoked, allowing for lazy evaluation of inner JSX.
`js`
renderIfElse(1 + 1 === 2)(
)
`jsx`
class MyComponent extends Component {
render() {
return (
{renderIfElse(1 + 2 === 3)(
The universe is working,The universe is broken
)}
);
}
}
`jsx`
class MyComponent extends Component {
render() {
return (
{renderIfElse(1 + 2 === 3)(
() => (
This is only invoked if the universe is working
),
() => (
This is only invoked if the universe is broken
)
)}
);
}
}
`jsx`
class MyComponent extends Component {
render() {
const isTheUniverseIsWorking = renderIfElse(1 + 2 === 3);
return (
{isTheUniverseIsWorking(
The universe is still wroking,
The universe is not wroking
)}
)
}
}
jsx
const isEven = number => renderIfElse(number % 2 === 0);class MyComponent extends Component {
render() {
return (
{isEven(this.props.count)(
{this.props.count} is even,
{this.props.count} is odd
)}
);
}
}
`What it no longer looks like
`jsx
class MyComponent extends Component {
render() {
var conditionalOutput;
if (1 + 1 === 2) {
conditionalOutput = I am rendered!;
} else {
conditionalOutput = I am not rendered :(;
}
return (
{conditionalOutput}
{1 + 1 === 2 && I am rendered!}
{this.anotherConditionalRender()}
);
}
anotherConditionalRender() {
if (1 + 1 === 2) {
return I am rendered!
}
}
}
``