Skip to content

Instantly share code, notes, and snippets.

@duggiemitchell
Last active September 9, 2019 14:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save duggiemitchell/e18894b38a591afc30acfee8a3e4b2fb to your computer and use it in GitHub Desktop.
Save duggiemitchell/e18894b38a591afc30acfee8a3e4b2fb to your computer and use it in GitHub Desktop.
Timer component written as a class
class Timer extends React.Component {
constructor(props) {
super(props);
this.state = { seconds: 0 };
}
tick = () => {
this.setState(state => ({
seconds: state.seconds + 1
}));
};
componentDidMount() {
this.interval = setInterval(() => this.tick(), 1000);
}
componentWillUnmount() {
clearInterval(this.interval);
}
render() {
return <div>Seconds: {this.state.seconds}</div>;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment