Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ...
- class Clock extends React.Component {
- constructor(props) {
- super(props);
- this.state = {
- timerID: null,
- timer: 0
- };
- };
- componentDidMount() {
- console.log("Clock", "componentDidMount");
- const timerID = setInterval( this.updateTimer, 1000);
- this.setState( { timerID : timerID} );
- }
- componentWillUnmount() {
- console.log("Clock", "componentWillUnmount");
- clearInterval(this.state.timerID);
- }
- updateTimer = () => {
- this.setState( {timer: this.state.timer + 1 } );
- }
- render() {
- return (
- <div>
- <h1>Clock!</h1>
- <h3>{this.state.timer} s</h3>
- </div>
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement