-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDigitalClock.js
36 lines (36 loc) · 927 Bytes
/
DigitalClock.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import React,{Component} from 'react';
class DigitalClock extends Component{
constructor(props){
super(props);
this.state={
time:" "
}
}
componentDidMount(){
this.tick();
}
tick=()=>{
const hours=new Date().getHours();
const minutes=new Date().getMinutes();
const seconds=new Date().getSeconds();
const UpdatedTime=`${hours}:${minutes}:${seconds}`;
this.setState({time:UpdatedTime});
};
componentDidUpdate(prevProps,prevState){
if(this.state.time!=prevState.time){
this.interval=setInterval(()=>{
this.tick();
},1000);
}
}
componentWillUnmount(){
clearInterval(this.interval);
}
render(){
return<div>
<h1>DigitalClock</h1>
<h1>{this.state.time}</h1>
</div>
}
}
export default DigitalClock;