Answer the question
In order to leave comments, you need to log in
How to display data with api backend?
There is a component where I want to display data from an existing api.
I tried to output data like this, purely in the console:
useEffect(() => {
fetch('https://xxx')
.then(response => response.json())
.then(data => console.log(data))
}, [])
export default function ListItems() {
const [users, setUsers] = useState(null)
const URL = 'xxx'
// const fetchData = async () => {
// const response = await axios.get(URL)
// setUsers(response.data)
// }
useEffect(() => async () => {
const response = await axios.get(URL)
setUsers(response.users)
})
return (
<ul className="list">
{
users && users.map(user => {
return (
<li className="item" key={ user.id }>
<img className="item__photo" src={ user.photo } alt="user-1"/>
<div className="item__name">
<h3 className="subtitle">{user.name}</h3>
</div>
<div className="item__description">
<h3 className="description">{user.position}</h3>
<h3 className="description">{user.email}</h3>
<h3 className="description">{user.phone}</h3>
</div>
</li>
)
})
}
</ul>
)
}
Answer the question
In order to leave comments, you need to log in
useEffect(() => async () => {
const response = await axios.get(URL)
setUsers(response.users)
})
useEffect(() => {
const fetchData = async () => {
const response = await axios.get(URL);
setUsers(response.users);
};
fetchData();
}, []);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question