this.setState does not update state

前端 未结 2 1017
野趣味
野趣味 2021-01-28 18:20

I\'m trying to use this.setState within handleFormSubmit however this.setState isn\'t updating and I\'m not sure why. If I run conso

2条回答
  •  时光取名叫无心
    2021-01-28 18:53

    Keep in mind that the state isn't updated immediately. If you want to check if it's updated use callback function. Something as follows:

    this.setState({ careerHistoryPositions: updatePosition }, () => console.log(this.state.careerHistoryPositions);
    

    From the docs :

    setState() does not immediately mutate this.state but creates a pending state transition. Accessing this.state after calling this method can potentially return the existing value. There is no guarantee of synchronous operation of calls to setState and calls may be batched for performance gains.

    Hope this helps.

提交回复
热议问题