import React from ‘react‘
class Clock extends React.Component {
constructor(props) {
super(props)
this.state = {
date: new Date()
}
}
componentDidMount() {
this.timer = setInterval(() => {
this.setState({
date: new Date()
})
}, 1000)
}
// componentDidUpdate(currentProps, currentState) { // 接受2个参数 第一个参数 props 第二个参数 变化的值
// console.log(currentState)
// }
componentWillUnmount() {
clearInterval(this.timer)
}
render() {
return (
<div className="jumbotorn">
<h1>{ this.state.date.toLocaleTimeString() }</h1>
</div>
)
}
}
export default Clock