Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
var Timer = React.createClass({
getDefaultProps: function() {
return {label: "Timer"}
},
getInitialState: function() {
return {secondsElapsed: 0};
},
tick: function() {
this.setState({secondsElapsed: this.state.secondsElapsed + 1});
},
componentDidMount: function() {
this.interval = setInterval(this.tick, 1000);
},
componentWillUnmount: function() {
clearInterval(this.interval);
},
render: function() {
return (
{this.props.label}
Seconds Elapsed: {this.state.secondsElapsed}
);
}
});
React.render(<Timer label={"Sample timer"}/>, mountNode);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment