Skip to content

Instantly share code, notes, and snippets.

@jesty
Last active November 2, 2015 10:37
Show Gist options
  • Save jesty/9d2e47d8384d35d7e4ef to your computer and use it in GitHub Desktop.
Save jesty/9d2e47d8384d35d7e4ef to your computer and use it in GitHub Desktop.
A simple widget to define conditions (if) in JSX
import React from 'react';
export default class If extends React.Component {
constructor(props)
{
super(props);
this.state = {condition: this.props.condition};
}
componentWillReceiveProps(newProps)
{
if (this.props.condition != newProps.condition)
{
this.setState({condition: newProps});
}
}
render()
{
var result;
if (this.state.condition)
{
result = <span>{this.props.children}</span>;
} else
{
result = <span>{this.props.else}</span>;
}
return result;
}
}
If.propTypes = {
condition: React.PropTypes.any.isRequired,
else: React.PropTypes.element.isRequired
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment