In my ReactJS project, I use fetch the do the async processing, and after fetch the data,
I want to setState to change my local state. But I get the error return.
Uncaught (in promise) TypeError: Cannot read property 'setState' of undefined
Fetch function code:
AddDeal(deal){
fetch('shops/1/deals',{
method: "POST",
body: JSON.stringify(deal),
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
}).then(response => {
response.json().then(data =>{
this.setState({deals: data}); // and use this.props.dispatch I get `props` of undefined
})
})
}
I have watched the other question like mine React.js: loading JSON data with Fetch API and props from object array
So how can I fix it?
AddDealmethod?AddDeal?thistoAddDeal()? Would be good to include how you executeAddDeal()bind.this.AddDeal(). Let me try