Skip to content

Instantly share code, notes, and snippets.

@dsheiko
Last active January 19, 2019 22:47
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
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
/*
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