react学习---day05--React生命周期(新)
对比旧的生命周期,弃用了3个钩子与新增了2个钩子
弃用的函数钩子: 其实也是可以直接使用,但是react会发出警告,因为react准备异步渲染的工程,在此钩子中可能会造成不必要的bug,在未来可能会被弃用;例如: UNSAFE_componentWillMount; 需要加上UNSAFE_
- componentWillMount ===> UNSAFE_componentWillMount
- componentWillReceiveProps ===> UNSAFE_componentWillReceiveProps
- componentWillUpdate ===> UNSAFE_componentWillUpdate
新增的函数钩子: 用于罕见的用例
-
getDerivedStateFromProps: 会在调用 render 方法之前调用,并且在初始挂载及后续更新时都会被调用。它应返回一个对象来更新 state,如果返回
null则不更新任何内容。 - getSnapshotBeforeUpdate: 在最近一次渲染输出(提交到 DOM 节点)之前调用。它使得组件能在发生更改之前从 DOM 中捕获一些信息(例如,滚动位置)。此生命周期的任何返回值将作为参数传递给componentDidUpdate()。
可以在https://projects.wojtekmaj.pl/react-lifecycle-methods-diagram React官网推出的生命周期图中了解详细的内容
旧的生命周期图:

新的生命周期图


浙公网安备 33010602011771号