Skip to content

Instantly share code, notes, and snippets.

@yocontra
Created July 31, 2014 19:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yocontra/f4f6382a761756c9e49c to your computer and use it in GitHub Desktop.
Save yocontra/f4f6382a761756c9e49c to your computer and use it in GitHub Desktop.
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