I'm not sure what I'm doing wrong or what I'm missing but I can't iterate over my array of objects when I do my get request. I get the results on my console but they don't appear on the page unless I call one individual item. It only happens when I'm using React, I have also tried with JSON placeholder fake APIs and get the same result. Is there anything in React that stops this iteration to be executed? Thank you so much for your help!
import React, { Component } from 'react';
class UserList extends Component {
constructor(props) {
super(props);
this.state = {
users: [],
isLoaded: false,
};
}
componentDidMount() {
this.loadData();
}
loadData = async () => {
const response = await fetch('http://dev.pubmate.io/pubmate/api/0.1/user/all');
if (response) {
const allusers = await response.json();
console.log(response);
console.log(allusers);
console.log(allusers[0].email);
this.setState({
isLoaded: true,
users: [...allusers],
});
}
};
render() {
let { isLoaded, users } = this.state;
if (!isLoaded) {
return <div>Loading...</div>;
} else {
return (
<div className="userlist">
Data is been loaded
<ul>
{users.map((user) => {
<li key={user.id}>{user.title}</li>;
})}
</ul>
</div>
);
}
}
}
export default UserList;
.map(), hence nothing gets rendered.{users.map(user => <li key={user.id}> {user.title} </li>)}