1、當類組件被傳遞給 ReactDOM.render() 時,React調用類組件的構造函數,初始化一些參數變量
constructor(props) {
super(props);
this.state = {date: new Date()};
}
2、React 然後調用 Clock 組件的 render() 方法。這是 React 瞭解屏幕上應該顯示什麼內容,然後 React 更新 DOM 以匹配 Clock 的渲染輸出。
3、當類的輸出插入到 DOM 中時,會調用函數:componentDidMount() ,在裏邊調用接口獲得渲染數據
componentDidMount(){
console.log('--------componentDidMount-----------');
}
4、利用this.setState() 函數改變變量值,動態變化
this.setState({
date: new Date()
});
5、一旦Clock組件被從DOM中移除,React會調用componentWillUnmount()這個鉤子函數,做些清理工作
componentWillUnmount() {
clearInterval(this.timerID);
}