Skip to content

Instantly share code, notes, and snippets.

@erikthedeveloper
Last active April 26, 2016 11:31
Show Gist options
  • Select an option

  • Save erikthedeveloper/bbc22b59fa739fed46bf51b4dc24b2e8 to your computer and use it in GitHub Desktop.

Select an option

Save erikthedeveloper/bbc22b59fa739fed46bf51b4dc24b2e8 to your computer and use it in GitHub Desktop.
/**
* See http://facebook.github.io/react/docs/component-specs.html
*/
const SomeStatefulComponent = React.createClass({
// Define the initial "state" of our component
// Update state via this.setState({loading: true})
getInitialState() {
return {
loading: false,
};
},
// Make use of lifecycle hooks such as "request messages (Ajax) on did mount"
componentDidMount() {
const {messages, requestMessages} = this.props;
if (messages.length <= 1) {
requestMessages();
}
},
// ...
render() {
// We have access to this.props as well as this.state
return (
<div>
<h1>Hello, I am SomeStatefulComponent!</h1>
<h3>{this.props.message}</h3>
// Only render children when we are not loading
{this.state.loading ? 'Loading...' : this.props.children}
</div>
)
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment