timer.js
1.01 KB
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
37
38
39
import React, { Component } from "react"
import moment from "./lib/moment"
import "./timer.css"
export default class Timer extends Component {
constructor() {
super();
this.state = {
showDateTime: "",
classInfo: ""
}
}
static getDerivedStateFromProps(props, state) {
let { className } = props
if (className !== state.className) {
return {
classInfo: className
}
}
return null;
}
forShowDateTime() {
let { type = "YYYY-MM-DD HH:mm:ss" } = this.props;
this.setState({
showDateTime: moment().format(type)
})
}
componentDidMount() {
this.forShowDateTime()
this.timer = setInterval(() => {
this.forShowDateTime()
}, 1000)
}
render() {
let { classInfo, showDateTime } = this.state;
return (<div className="timerComCss">
<p className={classInfo}>{showDateTime}</p>
</div>)
}
}