Skip to content

Instantly share code, notes, and snippets.

@ashlynnpai
Last active December 8, 2017 16:32
Show Gist options
  • Save ashlynnpai/25fc583c0166837426c963e48ac176e6 to your computer and use it in GitHub Desktop.
Save ashlynnpai/25fc583c0166837426c963e48ac176e6 to your computer and use it in GitHub Desktop.
counter in React
//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