Skip to content

Instantly share code, notes, and snippets.

@matheusml
Last active June 21, 2017 17:38
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 matheusml/cade81d8762f98cff962c1eeb0a9acce to your computer and use it in GitHub Desktop.
Save matheusml/cade81d8762f98cff962c1eeb0a9acce to your computer and use it in GitHub Desktop.
import React from 'react';
export default class Timer extends React.Component {
constructor(props) {
super(props);
this.state = { elapsed: 0 };
this.tick = this.tick.bind(this);
}
componentDidMount() {
this.timer = setInterval(this.tick, 1000);
}
componentWillUnmount() {
clearInterval(this.timer);
}
tick() {
this.setState({ elapsed: this.state.elapsed + 1 });
}
render() {
return <h1>{this.state.elapsed} seconds</h1>;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment