1 componentDidMount() {
2 this.intervalID = setInterval(
3 () => this.tick(),
4 1000
5 );
6 }
7 componentWillUnmount() {
8 clearInterval(this.intervalID);
9 }
10
1class Clock extends React.Component {
2 constructor(props) {
3 super(props);
4 this.state = {
5 time: new Date().toLocaleString()
6 };
7 }
8 componentDidMount() {
9 this.intervalID = setInterval(
10 () => this.tick(),
11 1000
12 );
13 }
14 componentWillUnmount() {
15 clearInterval(this.intervalID);
16 }
17 tick() {
18 this.setState({
19 time: new Date().toLocaleString()
20 });
21 }
22 render() {
23 return (
24 <p className="App-clock">
25 The time is {this.state.time}.
26 </p>
27 );
28 }
29}
30