import React, { useEffect, useState } from 'react';
import axios from "axios";
import './List.css';
import { toast } from "react-toastify";
const List = () => {
const url = "http://localhost:4500";
const [list, setList] = useState([]);
const fetchList = async () => {
const response = await axios.get(`${url}/api/food/list`);
console.log("the response data", response.data);
if (response.data.success) {
setList(response.data.data)
}
else {
toast.error("error")
}
}
useEffect(() => {
fetchList();
}, []);
useEffect(() => {
console.log("the list type: ", typeof (list));
}, [list]);
return (
<div className='list add flex-col'>
<p>All food List</p>
<div className="list-table">
<div className="list-table-format title">
<b>Image</b>
<b>Name</b>
<b>Category</b>
<b>Price</b>
<b>Action</b>
</div>
{list.map((item,index)=>{
return(
<div key={index} className="list-table-formate">
<p>{item.name}</p>
</div>
)
})}
</div>
</div>
);
};
export default List;
,
In Above code when we insert the back-end api data in useState([]) empty array using fechList function so after inserted data list is empty.
if someone have Ans this problem so please discuss with me.
And also database api is respond the data properly.
I Axpecting that list is print in front-end and showing the data in front-end.
2
Answers
From your code, it seems like you’re new to React and issues like these are handled in a certain manner. What improvements does it require?
console.log()
) to get an idea.An updated, proper and scalable code will look something like this-
If you still have any queries, leave a comment, I’ll help.
The only thing that comes to my mind is that the response structure is different that the one that you expect here
try to check the response and see if it is ‘data.data’