What the difference of this.state and this.setstate in ReactJS?

Viewed 46700

I want to change the value for the hasSubmit key, like in the First Code section. I know this is not recommended. But the second code is asynchronous and I don't want to use the callback function of setState.

  • What is the difference of this.state and setState?
  • Is there any way to change state value hasSubmit immediately?

First Code:

this.state.hasSubmit = false
this.setState({})
//Code that will use `hasSubmit`.

Second code:

this.setState({
   hasSubmit: false,
});
//Code that will use `hasSubmit`.

ADD:

The scenario is that:

  1. hasSubmit set false in getInitialState().
  2. hasSubmit will change to false when I click submit button.
  3. hasSubmit will change to true when submitted.

First click submit has no problem and hasSubmit will be set to true.

But second click submit will be wrong using the Second asynchronous code, because the hasSubmit is still true, while the First Code can resolve the problem.

5 Answers
Related