Skip to content

Commit

Permalink
make the load more data button work
Browse files Browse the repository at this point in the history
  • Loading branch information
No0ne003 committed Mar 20, 2024
1 parent 30939ed commit abe19a5
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/pages/LoadMoreData.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ function LoadMoreData() {
const result = await response.json();

if (result && result.products && result.products.length) {
setProducts(result.products);
setProducts((prevData) => [...prevData, ...result.products]);
setLoading(false);
}
console.log(result)
} catch (e) {
console.log(e);
setLoading(false);
Expand All @@ -27,7 +28,7 @@ function LoadMoreData() {

useEffect(() => {
fetchProducts();
}, []);
}, [count]);

if (loading) {
return <div>Loading data ! Please wait man</div>;
Expand All @@ -39,10 +40,10 @@ function LoadMoreData() {
>
<div className="grid grid-cols-1 md:grid-cols-3 lg:grid-cols-4 gap-3">
{products && products.length
? products.map((item) => (
? products.map((item, index) => (
<div
className="p-5 border-border border-2 flex flex-col items-center justify-center gap-3"
key={item.id}
key={index}
>
<img src={item.thumbnail} alt={item.title} />
<p className="font-mono">{item.title}</p>
Expand All @@ -51,7 +52,9 @@ function LoadMoreData() {
: null}
</div>
<div className="btn-container">
<Button variant="secondary">Load More Products</Button>
<Button onClick={() => setCount(count + 1)} variant="secondary">
Load More Products
</Button>
</div>
</div>
);
Expand Down

0 comments on commit abe19a5

Please sign in to comment.