Can i know about any pending state transition in react before performing an operation?

687 views Asked by At

I have to perform an operation in my code.This operation involves getting values from my state.In certain cases, my state returns the old value. Is there any function that can help me to trigger my code after all pending state transitions are complete?

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

There are 1 answers

0
asafel On

You should use shouldComponentUpdate()

shouldComponentUpdate() is invoked before rendering when new props or state are being received. Defaults to true This method is not called for the initial render or when forceUpdate() is used.

read more here

So what you can do is:

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

}

Hope it helps.