Why can't I set the state to a state variable which I already defined in the state, I also tried to set state as this.state.propval = response.data.
State declaration,
constructor(props) {
super(props);
this.state = {
propval:null,
};
}
Sending the axios request and getting the response here.
axios.post('/eventcreate/createevent', data)
.then(function (response) {
alert(response.data)
var result = response.data
console.log("result out :"+result)
this.setState({propval : result})
this.propfun(response.data)
self.forceUpdate()
})
But getting the error
Unhandled Rejection (TypeError): Cannot read property 'setState' of undefined
(anonymous function)
src/components/forms/neweventcreation.js:306
303 | alert(response.data)
304 | var result = response.data
305 | console.log("result out :"+result)
> 306 | this.setState({propval : result})
307 |
308 | this.propfun(response.data)
309 | self.forceUpdate()
Please help me to figure this out.