Last active
November 2, 2015 10:37
-
-
Save jesty/9d2e47d8384d35d7e4ef to your computer and use it in GitHub Desktop.
A simple widget to define conditions (if) in JSX
This file contains 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
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