when calling the API from server I am able to see the response but when using data.map()
to iterate the data is not reflected in frontend in the next js
First I called the API from the server side and I was able to see the response .but when I try to iterate the data using the map function I am not able to see the data and there is no error.
Here is my code.
async function productlist(){
let data = await fetch(`https://dummyjson.com/users`);
data = await data.json();
return data;
}
export default async function Home() {
let data = await productlist();
console.log("dataArr",data);
console.log("dataArr",data[0].urls.small);
return (
<div>
{/*<!-- Featured section -->*/}
<section className="py-20">
<div className="container mx-auto px-4">
<h2 className="text-3xl font-bold text-gray-800 mb-8">Free Stock Photos</h2>
<div className="grid grid-cols-1 md:grid-cols-3 gap-8">
{
data.map((product)=>{
<div className="bg-white rounded-lg shadow-md overflow-hidden opacity-100 hover:opacity-80 transition duration-500 ease-in-out">
<img src={product.firstName} alt="Coffee"
className="w-full h-full object-cover"/>
<h2>{product.firstName}</h2>
</div>
})}
</div>
</div>
</section>
</div>
)
}
2
Answers
your
.map
function is not returning anything.you either have use an explicit return:
or an implicit return:
Have a look here
Note: when using Array.prototype.map() inside JSX you have always to specify a unique
key
for each element of the returned array.