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
andsetState
? - 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:
hasSubmit
setfalse
ingetInitialState()
.
hasSubmit
will change tofalse
when I clicksubmit
button.
hasSubmit
will change totrue
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.