I am getting back-end data by using axios.get()
, but the array of objects is not populating the state.
It's giving me following in the console.
Outside Effect {success: true, message: 'All Categories List', category: Array(2)} [] Outside Effect[object Object],[object Object]
Back-end code is also given.
Front-end
const [categories,setCategories] =useState([])
const getAllCategory = async () => {
try {
const {data} = await axios.get("/api/v1/category/get-category");
if (data.success) {
console.log(data)
setCategories(data.category);
console.log(categories)
}
} catch (error) {
console.log(error);
toast.error("Something went wrong in getting category");
}
};
useEffect(() => {
getAllCategory();
}, []);
console.log("Outside Effect", categories)
Rendering
<table className="table">
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">Actions</th>
</tr>
</thead>
<tbody>
{categories.map((c)=>{
<tr>
<td key={c._id}>{c.name}</td>
</tr>
})}
</tbody>
</table>