import React, {Component} from 'react'
class Timer extends Component({
getInitialState: function(){
return{
seconds: 0
};
},
componentDidMount: function(){
this.timer = setInterval(this.tick, 1000);
},
tick: function(){
this.setState({seconds: this.state.seconds + 1});
},
componentWillUnmount: function(){
clearInterval(this.timer);
},
render: function(){
return(
<h5>Прошло: {this.state.seconds}</h5>
);
}
});
export default Timer
import React, { Component } from 'react';
class Timer extends Component {
state = {
seconds: 0
};
componentDidMount() {
this.timer = setInterval(this.tick, 1000);
}
tick() {
this.setState({ seconds: this.state.seconds + 1 });
}
componentWillUnmount() {
clearInterval(this.timer);
}
render() {
return <h5>Прошло: {this.state.seconds}</h5>
}
}
export default Timer;