Created
July 31, 2014 19:20
-
-
Save yocontra/f4f6382a761756c9e49c 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
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