(React学习笔记1)组件生命周期总结

221 阅读1分钟

图示

Y_FKMNHV0)[($T5VG1PF](Y.png

  • 只有类组件才有生命周期

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()组件卸载(从页面中消失)执行清理工作(比如:清理定时器等)

补充

以下还有没具体说明钩子函数,因为官方不推荐使用。

React生命周期.webp.jpg