File tree 3 files changed +34
-10
lines changed
3 files changed +34
-10
lines changed Original file line number Diff line number Diff line change 1
1
import React , { useState , useEffect } from 'react'
2
2
// 👉 STEP 2 - React Router imports (Route, Link and Switch)
3
- import { Link } from "react-router-dom" ;
3
+ import { Link , Route , Switch } from "react-router-dom" ;
4
4
// Link aka ahref
5
5
6
6
// Components used for the different routes
@@ -33,8 +33,27 @@ export default function App(props) {
33
33
{ /* 👉 STEP 3 - Make Links to navigate us Home (`/`) and Shop (`/items-list`) */ }
34
34
</ div >
35
35
</ nav >
36
-
36
+ < Switch >
37
+ < Route path = { "/items-list/:itemID" } >
38
+ < Item />
39
+ </ Route >
40
+ < Route path = "/items-list" >
41
+ < ItemsList items = { stock } />
42
+ </ Route >
43
+ < Route path = "/" >
44
+ < Home />
45
+ </ Route >
46
+ </ Switch >
37
47
{ /* 👉 STEP 4 - Build a Switch with a Route for each of the components imported at the top */ }
48
+
38
49
</ div >
39
50
)
40
51
}
52
+
53
+ /**
54
+ * switch (varName) {
55
+ * case: "blah":
56
+ * doSomething();
57
+ * break;
58
+ * }
59
+ */
Original file line number Diff line number Diff line change 1
1
import React from 'react'
2
2
// We'll need React Router's own version of the History API
3
+ import { useHistory } from "react-router-dom" ;
3
4
4
5
export default function Home ( ) {
5
6
// 👉 STEP 5 - Build a click handler that will imperatively
6
7
// navigate us to <website base URL>/items-list
8
+ const history = useHistory ( ) ;
7
9
8
10
const routeToShop = ( ) => {
9
-
11
+ history . push ( "/items-list" ) ;
10
12
}
11
13
12
14
return (
Original file line number Diff line number Diff line change 1
1
import React from 'react'
2
2
// We'll need a Link and the useRouteMatch hook from 'react-router-dom'
3
+ import { Link , useRouteMatch } from "react-router-dom" ;
3
4
4
5
export default function ItemsList ( props ) {
5
6
const { items } = props
6
7
7
8
// We'll grab the current URL using the hook
9
+ const { url } = useRouteMatch ( ) ;
8
10
9
11
return (
10
12
< div className = 'items-list-wrapper' >
@@ -14,14 +16,15 @@ export default function ItemsList(props) {
14
16
key = { item . id }
15
17
>
16
18
{ /* 👉 STEP 6 - Link starts, navigates us from <current url> to <current url>/<id of the item> */ }
17
- < img
18
- className = 'items-list-image'
19
- src = { item . imageUrl }
20
- alt = { item . name }
21
- />
22
- < p > { item . name } </ p >
19
+ < Link to = { `${ url } /${ item . id } ` } >
20
+ < img
21
+ className = 'items-list-image'
22
+ src = { item . imageUrl }
23
+ alt = { item . name }
24
+ />
25
+ < p > { item . name } </ p >
23
26
{ /* Link ends */ }
24
-
27
+ </ Link >
25
28
< p > ${ item . price } </ p >
26
29
</ div >
27
30
) ) }
You can’t perform that action at this time.
0 commit comments