I'm coding a React app and I encountered a problem.
I want to change the state of a buttonfrom Write to Loading... when I call the HandleApiCall
function and I want to mantain it until I get all the data from the Axios api call.
I'm using a class component
This the JSX code.
<form onSubmit={this.HandleApiCall.bind(this)}>
<div className="TextAreaContainer">
{ this.state.loading === false ?
<button className= "btn_" type="submit" >Write</button> :
<button className= "btn_" type="submit">Loading...</button>
}
</div>
<div className="TextareaContainer">
<textarea
className= "textarea"
placeholder="write the description here"
value={this.state.input}
onChange={(ev) => {
this.updateBody(ev.target.value)
}
}>
</textarea>
</div>
</form>
This is the HandleApiCall funtion
HandleApiCall(e) {
e.preventDefault();
await this.setState({ loading : true });
axios.post(TRANSLATE_API_URL, body).then(({ data: { text } }) => {//some code here});
}
I tried to do in that way but it does not work.
Any ideas?