Created
December 11, 2017 18:56
-
-
Save ashlynnpai/bbaaa4b99cdd0a4bb17ad0652da16fb9 to your computer and use it in GitHub Desktop.
Timer countdown using React
This file contains 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
class Timer extends React.Component { | |
constructor(props) { | |
super(props); | |
this.state = {count: 10}; | |
} | |
componentDidMount() { | |
this.timerID = setInterval( | |
() => this.tick(), | |
1000 | |
); | |
} | |
doSomething() { | |
if (this.state.count == 0) { | |
clearInterval(this.timerID); | |
} | |
} | |
tick() { | |
this.setState({ | |
count: this.state.count - 1 | |
}); | |
this.doSomething(); | |
} | |
render() { | |
return ( | |
<div> | |
<h1>{this.state.count}</h1> | |
</div> | |
); | |
} | |
} | |
ReactDOM.render( | |
<Timer />, | |
document.getElementById('app') | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment