Skip to content

Instantly share code, notes, and snippets.

@ashlynnpai
Created December 11, 2017 18:56
Show Gist options
  • Save ashlynnpai/bbaaa4b99cdd0a4bb17ad0652da16fb9 to your computer and use it in GitHub Desktop.
Save ashlynnpai/bbaaa4b99cdd0a4bb17ad0652da16fb9 to your computer and use it in GitHub Desktop.
Timer countdown using React
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