File tree Expand file tree Collapse file tree 1 file changed +44
-4
lines changed Expand file tree Collapse file tree 1 file changed +44
-4
lines changed Original file line number Diff line number Diff line change 1
- import React from 'react'
2
- import HelloWorld from '../Components/HelloWorld'
1
+ import React , { useState , useEffect } from 'react'
2
+ import axios from 'axios'
3
+ import ProductCard from '../Components/ProductCard'
3
4
4
5
function Home ( ) {
6
+ let content = null
7
+ const url = `https://5f2a8d0d6ae5cc0016422a91.mockapi.io/api/v1/products?page=1&limit=10`
8
+ const [ products , setProducts ] = useState ( {
9
+ data : null ,
10
+ error : false
11
+ } )
12
+
13
+ useEffect ( ( ) => {
14
+ axios . get ( url )
15
+ . then ( res => {
16
+ setProducts ( {
17
+ data : res . data ,
18
+ error : false
19
+ } )
20
+ } )
21
+ . catch ( ( ) => {
22
+ setProducts ( {
23
+ data : null ,
24
+ error : true
25
+ } )
26
+ } )
27
+ } , [ url ] )
28
+
29
+
30
+ if ( products . error ) {
31
+ content = < p >
32
+ There was an error, please refresh or try again later!
33
+ </ p >
34
+ }
35
+
36
+ if ( products . data ) {
37
+ content =
38
+ products . data . map ( ( product ) =>
39
+ < ProductCard key = { product . id }
40
+ product = { product }
41
+ />
42
+ )
43
+ }
44
+
5
45
return (
6
46
< div >
7
- < h1 className = "font-bold text-2xl" > Home page </ h1 >
8
- < HelloWorld name = "Drake" />
47
+ < h1 className = "font-bold text-2xl" > Best Sellers </ h1 >
48
+ { content }
9
49
</ div >
10
50
)
11
51
}
You can’t perform that action at this time.
0 commit comments