Reactjs 如何将componentWillUpdate更改为getSnapshotBeforeUpdate
我正在将应用程序更新为React 15到React 16,控制台中有关于 警告:componentWillUpdate已重命名,不建议使用 代码中是这样的:Reactjs 如何将componentWillUpdate更改为getSnapshotBeforeUpdate,reactjs,Reactjs,我正在将应用程序更新为React 15到React 16,控制台中有关于 警告:componentWillUpdate已重命名,不建议使用 代码中是这样的: 组件将更新(下一步,下一步状态){ if(nextState.isOpen===this.state.isOpen)返回; 如果(nextState.isOpen)this.showsslider(); 否则,这个.hideSlider(); } 如何将其重写为新的生命周期方法getSnapshotBeforeUpdate 我尝试了下一种
组件将更新(下一步,下一步状态){
if(nextState.isOpen===this.state.isOpen)返回;
如果(nextState.isOpen)this.showsslider();
否则,这个.hideSlider();
}
如何将其重写为新的生命周期方法getSnapshotBeforeUpdate
我尝试了下一种方法:
getSnapshotBeforeUpdate(下一步,下一步状态){
如果(nextState.isOpen)返回此.showsslider();
返回这个.hideSlider();
}
但这似乎不对,也不起作用
提前感谢您的帮助。getSnapshotBeforeUpdate的参数用于上一个道具和状态,而不是下一个道具和状态。因此,代码应该如下所示:
getSnapshotBeforeUpdate(prevProps, prevState) {
if (prevState.isOpen === this.state.isOpen) return;
if (this.state.isOpen) this.showSlider();
else this.hideSlider();
}
你根本不需要它。。。一致地渲染滑块。。。JSX:
{this.state.isOpen&&}
这是可行的,但我在控制台中遇到一个错误:可折叠的.getSnapshotBeforeUpdate():必须返回快照值(或null)。您返回了未定义的代码>如果(prevState.isOpen===this.state.isOpen)返回,则可能在此行中代码>,您没有返回任何内容。因此,尝试使用返回null代码>