React-生命周期-作用
constructor 生命周期方法中做什么
- 通过
props
接收父组件传递过来的数据 - 通过
this.state
初始化内部的数据 - 通过
bind
为事件绑定实例 (this)
render 生命周期方法中做什么
- 返回组件的网页结构
componentDidMount 生命周期方法中做什么
- 依赖于 DOM 的操作可以在这里进行
- 在此处发送
网络请求
就是最好的地方(官方建议) - 可以在此处添加一些订阅(会在
componentWillUnmount
取消订阅)
componentDidUpdate 生命周期方法中做什么
- 可以在此对更新之后的组件进行操作
componentWillUnmount 生命周期方法中做什么
- 在此方法中执行必要的清理操作
- 例如,清除
timer
,取消网络请求或清除 - 在
componentDidMount()
中创建的订阅等