❶ 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。举例: