Javascript 在执行操作之前,我可以知道react中的任何挂起状态转换吗?

Javascript 在执行操作之前,我可以知道react中的任何挂起状态转换吗?,javascript,reactjs,javascript-framework,Javascript,Reactjs,Javascript Framework,我必须在代码中执行一个操作。此操作涉及从我的状态获取值。在某些情况下,我的状态返回旧值。是否有任何函数可以帮助我在所有挂起的状态转换完成后触发代码 save : function(){ //here am using state and it doesnt reflect new value saveFunc(this.state.type); } 您应该使用shouldComponentUpdate() 当接收到新的道具或状态时,在呈现之前调用shouldComponen

我必须在代码中执行一个操作。此操作涉及从我的状态获取值。在某些情况下,我的状态返回旧值。是否有任何函数可以帮助我在所有挂起的状态转换完成后触发代码

  save : function(){
    //here am using state and it doesnt reflect new value
    saveFunc(this.state.type);
}

您应该使用
shouldComponentUpdate()

当接收到新的道具或状态时,在呈现之前调用shouldComponentUpdate()。默认值为true初始渲染或使用forceUpdate()时不调用此方法

因此,您可以做的是:

shouldComponentUpdate(nextProps, nextState) {
  if(this.state.type !== nextState.type) {
     // Do what you want
  }

}
希望有帮助。

组件生命周期: