Last active
January 19, 2019 22:47
-
-
Save dsheiko/c3f2c5f168d3bc2a707ce76afe641d77 to your computer and use it in GitHub Desktop.
Renders React components conditionally e.g. <If exp={ true }></If> to avoid inline short-circuit evaluation/ternary hell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Usage: | |
<If exp={ true }> components to render </If> | |
<If exp={ false }> components not to render </If> | |
*/ | |
import React from "react"; | |
import PropTypes from "prop-types"; | |
export default class If extends React.Component { | |
static propTypes = { | |
exp: PropTypes.any, | |
children: PropTypes.any | |
} | |
render() { | |
// JavaScript is an eagearly evaluated language, so even if th exp | |
const defer = () => this.props.children; | |
return this.props.exp ? defer() : null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment