❶ React class組件詳解
React class組件詳解
一. class 組件創建方式
二. Props 外部數據
三、state 和 setState 內部數據
(1) 初始化 state
(2) 讀寫 state
setState 不會立刻改變 this.state,而會在當前代碼運行完畢後,再去更新 this.state,從而觸發 UI 更新
這種狀態下,state 會立刻執行,fn 在寫入時執行。寫入時會 shallow merge,setState 會自動將 state 和舊 state 進行一級合並。
四、生命周期(可參考:React 生命周期)
生命周期函數列表:
1、constructor
2、shouldComponentUpdate
代碼:
3、render
return (
...)
縮寫 <>
4、componentDidMount
5、componentDidUpdate()
參數:componentDidUpdate(prevProps, prevState, snapshot)
prevProps 表示之前的 props,prevState 表示之前的 state
6、componenWillUnmount()
用途:組件將要被移出頁面然後被銷毀時執行代碼,Unmount 過的組件不會再次 mount。舉例: