am trying to display the first name of a user from a fetched api data... What could be wrong with this Reactjs code? i confirmed there was data already fetched from the api, but in displaying it... it returns blank and no errors
class App extends Component
constructor() {
super()
this.state = {
loading: false,
user: {}
}
}
componentDidMount() {
this.setState({loading: true})
fetch("https://reqres.in/api/users/2")
.then(response => response.json())
.then(data => {
this.setState({
loading: false,
user: data
})
})
}
render() {
const text = this.state.loading ? "loading..." : this.state.user.first_name
return (
<div>first name: {text}</div>
)
}
}
export default App
datalook like in.then(data => {this.state = {loading: true}and then removingthis.setState({loading: true})incomponentDidMountdata.itemsordata.data. (One of these should work)