I am trying to build a basic website that fetches a user list online and displays their names. Instead of using fetch.then, I put the fetch function within an async function and update the state within that very same function. The function is placed in componentDidMount. However, I received the error cannot read properties of undefined (reading 'setState'). I have tried changing to fetch.then() and it works. Could you tell me the reason behind this? Thank you very much
import React, {Component} from 'react'
class App extends Component{
constructor (){
super()
this.state = {
monsterList: []
}}
componentDidMount(){
async function fetchingData(){
let data = await fetch('https://jsonplaceholder.typicode.com/users')
data = await data.json()
this.setState(()=>{
return {monsterList: data}
})
}
fetchingData()
}
render() {
return (
<div className='App'>
{this.state.monsterList.map(monster => {
return (
<div key={monster.id}>
<h1>{monster.name}</h1>
</div>
)
})}
</div>
)
}
}
export default App;