As in the title, I'm confused about the behavior of fetch calling the setState method in a React component.
This way it works:
fetch(url)
.then((response) => {
return response.json()
})
.then((json) => {
this.setState({page: json});
});
This way it fails:
fetch(url)
.then(function (response) {
return response.json()
}).then(function (json) {
console.log('parsed json', json);
this.setState({page: json}); // Cannot read property 'setState' of undefined
}).catch(function (ex) {
console.log('parsing failed', ex)
});
What's the reason of such behavior?