react/docs/_js/examples/timer.js

29 lines
647 B
JavaScript
Raw Normal View History

var TIMER_COMPONENT = `
var Timer = React.createClass({
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 (
<div>Seconds Elapsed: {this.state.secondsElapsed}</div>
);
}
});
React.render(<Timer />, mountNode);
`;
2013-05-30 03:46:11 +08:00
React.render(
2013-05-30 03:46:11 +08:00
<ReactPlayground codeText={TIMER_COMPONENT} />,
document.getElementById('timerExample')
);