JSFiddle - React, Tailwind, and code Playground
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.12.2/JSXTransformer.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.12.2/react.min.js"></script>
<div id="example"></div>
<script type="text/jsx">
var Timer = React.createClass({
getInitialState: function() {
//Intialize component with state as 0 seconds
return {secondsElapsed: 0};
},
tick: function() {
//increment the timer and update the state
this.setState({secondsElapsed: this.state.secondsElapsed + 1});
},
componentDidMount: function() {
//Start the timer when the component is loaded in DOM
this.interval = setInterval(this.tick, 1000);
},
componentWillUnmount: function() {
//Clear the timer when the component is removed from DOM
clearInterval(this.interval);
},
render: function() {
return (
<div>Seconds Elapsed: {this.state.secondsElapsed}</div>
);
}
});
React.render(<Timer />, document.getElementById("example"));
</script>