timer.js 1.01 KB
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>)
    }
}