this.setState does not update state

前端 未结 2 1014
野趣味
野趣味 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:55

    You should show how you are calling handleFormSubmit chances are that it's bound to a Dom event. So this is not the class/component, instead if you console.log(this); you'll see that it's the Dom element, the form element.

    To make your code work as intended, in your component constructor() method, add this to rebind the handler function to the react component's class method, and you'll have access to this.state and this.setState()

    this.handleFormSubmit = this.handleFormSubmit.bind(this);
    

提交回复
热议问题