I am trying to get data using axios but it doesn’t seem to load onto the page. I have used the link in postman and retrieves all the data so I assume the get request from the server is fine but when used in the jsx file it shows no data
here is my jsx file
index.js
import React from "react";
import { useState, useEffect } from "react";
import axios from 'axios'
export default function App() {
const client = axios.create({
baseURL: "http://localhost:8080/api/getUser"
});
const [users, setPosts] = useState([]);
useEffect(() => {
client.get('?_limit=10').then((response) => {
setPosts(response.data);
});
}, []);
return (
<div className="app">
<h2>All Users 📫</h2>
{users && users.length > 0 && users.map(({id, post}) => {
return (
<div className="post-card" key = {id}>
<h2 className="post-title">{post.email}</h2>
<p className="post-body">{post.firstName}</p>
<div className="button">
<div className="delete-btn">Delete</div>
</div>
</div>
);
})}
</div>
);
}
2
Answers
Check your response data structures before updating the
App
component state.Postcard UI can be shown if the item of the
users
array exists.I looking at the previous answer, and I think it can be fixed with the below codes.
To have answer instead of comments:
Error was due to data from API was coming with different structure, as per comments,
JSON.stringify(response.data)
wasSo in order to set a list to a state the correct code should look like that:
Next issue: destructuring of the map function was invalid. You have to destructure the exact properties you have in your object: