图示
- 只有类组件才有生命周期
1.组件创建时(挂载到页面上)
constructor() --> render() --> componentDidMount()
| 钩子函数 | 触发 | 作用 |
|---|---|---|
| construtor() | 创建组件时,最先执行 | 1.初始化state 2.为事件处理程序绑定this |
| render() | 每次渲染组件都会触发 | 渲染UI(注意不能使用setState,会造成递归更新调用render()) |
| componentDidMount() | 组件挂载(完成DOM渲染)后 | 1.发送ajax请求 2.DOM操作 |
2.组件更新阶段
render() --> componentDidUpdate()
| 钩子函数 | 触发 | 作用 |
|---|---|---|
| render() | 每次组件渲染都会触发 | 渲染UI(与 挂在阶段 是同一个render) |
| componentDidUpdate() | 组件更新(完成DOM)后 | 1.发送ajax请求 2.DOM操作 (注意:如果使用setState需要房子if判断中) |
3.组件卸载阶段
componentWillUnmount()
| 钩子函数 | 触发 | 作用 |
|---|---|---|
| componentWillUnmount() | 组件卸载(从页面中消失) | 执行清理工作(比如:清理定时器等) |
补充
以下还有没具体说明钩子函数,因为官方不推荐使用。