I'm trying to update a state inside a array map, but it gives me an error that setState is not a function on this. How can I acheive this? My code snippet is given below
this.setState({
isLoading: true
});
this.state.questions.map(function(key, value) {
if (key.id === undefined) {
axios
.post("/api/questions/create", key)
.then(response => {
this.setState({
isLoading: true
});
})
.catch(error => {});
} else {
axios
.post("/api/questions/update/" + key.id, key)
.then(response => {
this.setState({
isLoading: true
});
})
.catch(error => {});
}
});
thisin a variable.thisvalues but in ES6 arrow functions bind(this) keywordthis.state.questions.map(function(key, value) { ...//stuff }).bind(this);