Skip to content

Commit 07d8c4f

Browse files
author
zuojing
committed
react生命周期
1 parent 3724eea commit 07d8c4f

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

2021/react_01.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@
1515
- 1. 找到共同的父组件,通过父组件的props传递
1616

1717

18-
18+
### redux
1919

2020

2021/react_03.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
## 组件生命周期
2+
组件的灵魂是render()
3+
#### 在react 15中的生命周期
4+
- 初始化渲染
5+
constructor() -> componentWillMount()->render()->componentDidMount()->componentWillUnmount()
6+
- 组件更新(父组件触发)
7+
componentWillReceiveProps()->shouldComponentUpdate()->componentWillUpdate()->render()->componentDidUpdate()->componentWillUnmount()
8+
- 组件更新(自身组件触发)
9+
shouldComponentUpdate()->componentWillUpdate()->render()->componentDidUpdate()->componentWillUnmount()
10+
- constructor()初始化state
11+
- componentWillMount()
12+
- render() 返回需要渲染的内容,不会去操作真实 DOM
13+
- componentDidMount() 真实dom已经挂载到页面上
14+
- componentWillReceiveProps() 并不是由 props 的变化触发的,而是由父组件的更新触发的。
15+
- componentWillUnmount() 组件销毁。 组件销毁的两个原因:组件在父组件中被移除;组件中设置了key,父组件在render过程中,发现key值和上次不一致。
16+
17+
#### 在react 16中的生命周期
18+
- 初始化阶段
19+
constructor()->getDerivedStateFromProps()->render()->componentDidMount()
20+
- 更新阶段 父组件触发
21+
getDerivedStateFromProps()->shouldComponentUpdate()->render()->getSnapshotBeforeUpdate()->componentDidUpdate()
22+
- 更新阶段 自身触发
23+
shouldComponentUpdate()->render()->getSnapshotBeforeUpdate()->componentDidUpdate()
24+
- static getDerivedStateFromProps(props, state) 静态方
25+
- getDerivedStateFromProps 是一个静态方法,没有this
26+
- getDerivedStateFromProps 可以接受两个参数props(来自父组件),state(自身)
27+
- getDerivedStateFromProps 需要返回一个对象格式(对象或者null),如果不,react会警告。因为react需要找个值来更新组件的state。它对 state 的更新动作并非“覆盖”式的更新,而是针对某个属性的定向更新。如果state不存在这个属性,那么state会增加这个属性,如果返回null或者{} 那么state还是原来的state
28+
- getDerivedStateFromProps 触发的原因1. setState, 2. forceUpdate
29+
- getDerivedStateFromProps 不能和componentWillReceiveProps划等号
30+
- 只用 getDerivedStateFromProps 来完成 props 到 state 的映射.确保生命周期函数的行为更加可控可预测
31+
- getSnapshotBeforeUpdate(prevProps, prevState) 的返回值会作为第三个参数给到componentDidUpdate。执行时机是render()之后,真实dom之前。
32+
- getSnapshotBeforeUpdate 可以同时获取新旧props和state,还有更新前的真实dom
33+
- getSnapshotBeforeUpdate 要想发挥作用,离不开 componentDidUpdate 的配合
34+
35+
#### Fiber机制下的生命周期分为render,pre-commit, commit阶段
36+
- render阶段是可以被暂停,终止或同步执行的。是异步的。因为该阶段对于用户来说是不被感知的。
37+
- pre-commit阶段可以读取DOM
38+
- commit 阶段可以使用DOM,运行副作用,安排更新。commit阶段总是同步执行的,因为用户能感知到视图的变化。
39+
40+
#### 将被废弃的生命周期
41+
componentWillMount,componentWillReceiveProps,componentWillUpdate。
42+
废弃的原因:因为Fiber机制下这三个生命周期都处于render阶段。而render阶段是允许暂停、终止和重启的,所以会导致它们可能被重复执行,如果开发者使用不当,那么会有bug。如 componentWillMount 里发起异步请求。在 componentWillxxx 被打断 + 重启多次后,就会发出多个异步请求。
43+
44+
45+

0 commit comments

Comments
 (0)