[react] 怎么定时更新一个组件?
生活随笔
收集整理的這篇文章主要介紹了
[react] 怎么定时更新一个组件?
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
[react] 怎么定時更新一個組件?
class Clock extends React.Component{constructor(props){super(props);this.state={date:new Date()};}componentDidMount(){this.timerID=setInterval(()=>this.tick(),1000);}componentWillUnmount(){clearInterval(this.timerID);}tick(){this.setState({date:new Date()});}render(){return (<div><h2>Timer {this.state.date.toLocaleTimeString()}.</h2></div>);}}ReactDOM.render(<Clock />,document.getElementById('root'));個人簡介
我是歌謠,歡迎和大家一起交流前后端知識。放棄很容易,
但堅持一定很酷。歡迎大家一起討論
主目錄
與歌謠一起通關前端面試題
總結
以上是生活随笔為你收集整理的[react] 怎么定时更新一个组件?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [react] React中你有使用过g
- 下一篇: 如何设置pycharm代码字体