# Demo
# 文件后缀
*.jsx
1
# Demo
import React, {Component} from 'react';
class Timer extends Component {
constructor(props) {
super(props);
this.state = {seconds: 0};
}
componentDidMount() {
this.interval = setInterval(() => {
this.setState(state => ({
seconds: state.seconds + 1,
}));
}, 1000);
}
componentWillUnmount() {
clearInterval(this.interval);
}
render() {
return <h1>Seconds: {this.state.seconds}</h1>;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24