Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
var Timer = React.createClass({
displayName: 'Timer',
propTypes: {
interval: React.PropTypes.number,
onTick: React.PropTypes.func
},
getDefaultProps: function() {
return {
interval: 1000
};
},
getInitialState: function() {
return {
ticksElapsed: 0
};
},
tick: function() {
this.setState({
ticksElapsed: this.state.ticksElapsed + 1
});
if (this.props.onTick) {
this.props.onTick();
}
},
componentDidMount: function() {
this.interval = setInterval(this.tick, this.props.interval);
},
componentWillUnmount: function() {
clearInterval(this.interval);
},
render: function() {
var txt = 'Seconds Elapsed: ' + this.state.ticksElapsed;
return React.DOM.div(null, txt);
}
});
var timer = Timer();
React.renderComponent(timer, mountNode);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.