Last active
April 26, 2016 11:31
-
-
Save erikthedeveloper/bbc22b59fa739fed46bf51b4dc24b2e8 to your computer and use it in GitHub Desktop.
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
| /** | |
| * 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