React中组件也有生命周期,也就是说也有很多钩子函数供我们使用, 组件的生命周期,我们会分为四个阶段,初始化、运行中、销毁、错误处理(16.3之后)
1、初始化
在组件初始化阶段会执行
-
constructor
-
static getDerivedStateFromProps()
-
componentWillMount() / UNSAFE_componentWillMount()
-
render()
-
componentDidMount()
2、更新阶段
props
或state
的改变可能会引起组件的更新,组件重新渲染的过程中会调用以下方法:
-
componentWillReceiveProps() / UNSAFE_componentWillReceiveProps()
-
static getDerivedStateFromProps()
-
shouldComponentUpdate()
-
componentWillUpdate() / UNSAFE_componentWillUpdate()
-
render()
-
getSnapshotBeforeUpdate()
-
componentDidUpdate()
3、卸载阶段
-
componentWillUnmount()
4、错误处理
-
componentDidCatch()
https://projects.wojtekmaj.pl/react-lifecycle-methods-diagram/
这里给大家一个网址可以查看一下React生命周期的图解,想了解React各大生命周期的详解请关注博主的下一条文章 谢谢大家的浏览