【发布时间】:2016-12-30 11:36:16
【问题描述】:
当我尝试使用与初始渲染相比不同的道具重新渲染反应组件时,我只能在调用渲染时看到更新的道具值。所有以前的生命周期方法都返回旧的 prop 值。
比如下面的代码……
componentWillReceiveProps() {
console.log("componentWillReceiveProps");
console.log(this.props.calls);
}
shouldComponentUpdate() {
console.log("shouldComponentUpdate");
console.log(this.props.calls);
return true;
}
componentWillUpdate() {
console.log("componentWillUpdate");
console.log(this.props.calls);
}
componentDidUpdate() {
console.log("componentDidUpdate");
console.log(this.props.calls);
}
render() {
console.log("render");
console.log(this.props.calls);
}
当使用新道具重新渲染时将返回...
componentWillReceiveProps
oldProp
shouldComponentUpdate
oldProp
componentWillUpdate
oldProp
render
newProp
componentDidUpdate
newProp
有谁知道为什么会发生这种情况并告诉我如何在渲染之前获取更新的道具?
【问题讨论】:
标签: reactjs