Skip to content

Instantly share code, notes, and snippets.

@subeshb1
Last active May 7, 2019 09:16
Show Gist options
  • Save subeshb1/9ccea92b5fd190c45d7a8914bcbd05f3 to your computer and use it in GitHub Desktop.
Save subeshb1/9ccea92b5fd190c45d7a8914bcbd05f3 to your computer and use it in GitHub Desktop.
class Timer extends React.Component {
constructor(props) {
super(props);
this.state = {
count: 0
};
}
componentDidMount() {
this.interval = setInterval(() => {
this.setState(({ count }) => ({ count: count + 1 }));
}, 1000);
}
componentWillUnmount() {
clearInterval(this.interval);
}
render() {
return (
<div>
<h1>Count: {this.state.count}</h1>
</div>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment