我的一个React组件接收一个JSON数组。该组件包含一个componentDidUpdate。我知道这个函数在渲染之前运行,我可以看到nextProps和nextState,所以我可以将它们与当前的比较。然而内部工作让我困惑componentDidUpdate内部工作使我困惑
下面是我的程序步骤:
- 父Component通过最近更新的列表,此组件
该组件运行componentDidUpdate:
- 将旧列表与此新的更新列表进行比较。找到新添加的项目,将其推入此组件的状态(数组调用
differenceList
)
- 将旧列表与此新的更新列表进行比较。找到新添加的项目,将其推入此组件的状态(数组调用
这个组件渲染根据
differenceList
以下是我的console.log,我已经全部摆放了。
//我触发列表
- 进入ComponentWillUpdate
- 的更新之后发现的唯一的项目:“1336”和下一行代码将它推入阵列
- 应该有只是把它用
self.setState({differenceList: self.state.differenceList.concat(item.id)});
- 达到了ComponentWillUpdate的末尾,this.state.differenceList是:[] //什么?
- 输入渲染函数,this.state.differenceList是:[]
- Entering ComponentWillUpdate // why why? ComponentWillUpdate的
- 端,this.state.differenceList是:[]
- 输入渲染功能,this.state.differenceList是:[ “1336”]
为什么#4
表示空?
at #6
为什么它再次进入componentWillUpdate?是因为我更新了componentWillUpdate中的状态吗?但是如果是的话为什么#5
显示为空状态#8
现在再次进入渲染函数后,它突然显示改变状态
我读了那个网站几次,不能相信我错过了重要的笔记。非常感谢!异步感谢我的感谢! – user308553