You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
33 lines
536 B
33 lines
536 B
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>
|
|
);
|
|
}
|
|
}
|
|
|
|
ReactDOM.render(
|
|
<Timer />,
|
|
document.getElementById('timer-example')
|
|
);
|