照着老师一模一样写的
怎么我的时间不变化,定时器没作用
拼写错误 componentDidMont 应该是 componentDidMount, 下次注意欧
代码贴来看看
import React, { Component } from 'react';
import Clock from './Clock';
class App extends Component {
render() {
return (
<div>
<Clock />
</div>
);
}
}
export default App;
import React from 'react';
class Clock extends React.Component{
constructor(props){
super(props);
this.state={
date:new Date()
}
}
componentDidMont(){
this.timer=setInterval( ()=>{
this.setState({
date: new Date()
})
}, 1000)
}
componentDidUpdate(currentProps,currentState){
console.log(currentState)
}
componentWillUnmount(){
clearInterval(this.timer)
}
render(){
return(
<h1>{this.state.date.toLocaleTimeString()}</h1>
)
}
}
export default Clock;
hao