Last active
December 8, 2017 16:32
-
-
Save ashlynnpai/25fc583c0166837426c963e48ac176e6 to your computer and use it in GitHub Desktop.
counter in 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
//reference: https://reactjs.org/docs/state-and-lifecycle.html | |
class Counter extends React.Component { | |
constructor(props) { | |
super(props); | |
this.state = {count: 0}; | |
} | |
componentDidMount() { | |
this.timerID = setInterval( | |
() => this.tick(), | |
1000 | |
); | |
} | |
componentWillUnmount() { | |
clearInterval(this.timerID); | |
} | |
tick() { | |
this.setState({ | |
count: this.state.count + 1 | |
}); | |
} | |
render() { | |
return ( | |
<div> | |
<h1>{this.state.count}</h1> | |
</div> | |
); | |
} | |
} | |
ReactDOM.render( | |
<Counter />, | |
document.getElementById('app') | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment