I'm having troubles setting state to the variable isCorrectAnswer
inside an axios call. I'm getting the error Cannot read properties of undefined (reading 'setState')
in the console log. What am I doing wrong?
Initial state of isCorrectAnswer
variable is constructor(props) {super(props)this.state = { isCorrectAnswer: false }}
and the axios call is
axios.post(
"API/path/to/ressource",
postData,
{
headers: {
"X-Access-Token":
"token",
},
}
)
.then(function (res) {
this.setState({
isCorrectAnswer: res.data.correct //Here im trying to change the state
}, () => console.log(this.state.isCorrectAnswer)) //and logging into console
})
.catch(function (error) {
console.log(error);
});