-
Notifications
You must be signed in to change notification settings - Fork 0
/
Home.jsx
50 lines (43 loc) · 1.15 KB
/
Home.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { useState, useEffect } from "react";
import Spinner from "../components/Spinner";
import Product from "../components/Product";
const Home = () => {
const API_URL = "https://fakestoreapi.com/products";
const [loading, setLoading] = useState(false);
const [posts, setPosts] = useState([]);
async function fetchProductData() {
setLoading(true);
try{
const res = await fetch(API_URL);
const data = await res.json();
setPosts(data);
}
catch(error) {
console.log("Error aagya ji");
setPosts([]);
}
setLoading(false);
}
useEffect( () => {
fetchProductData();
},[])
return (
<div>
{
loading ? <Spinner /> :
posts.length > 0 ?
(<div className="grid xs:gridcols-1 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4 max-w-6xl p-2 mx-auto space-y-10 space-x-5 min-h-[80vh]">
{
posts.map( (post) => (
<Product key = {post.id} post={post}/>
) )
}
</div>) :
<div className="flex justify-center items-center">
<p>No Data Found</p>
</div>
}
</div>
);
};
export default Home;