1
1
import React from 'react'
2
2
// We'll need quite a few imports from react-router-dom
3
+ import {
4
+ useParams ,
5
+ useRouteMatch ,
6
+ NavLink ,
7
+ Route ,
8
+ Switch
9
+ } from "react-router-dom" ;
3
10
4
11
import ItemDetails from './ItemDetails'
5
12
6
13
export default function Item ( props ) {
7
14
// We get ALL items through props. We'll use the URL to find out which item is the one to show.
8
15
const { items } = props
9
16
17
+ const { itemID } = useParams ( ) ;
18
+ const { url, path } = useRouteMatch ( ) ;
19
+ console . log ( url , path ) ;
20
+
10
21
// 👉 STEP 7 - We need to pull item from items, using a parameter in the URL (:itemID)
11
22
// Beware! The ids are integers, whereas URL parameters are strings.
12
23
// Beware! The JSX is expecting 'item' to exist instantly!
13
24
// we use this hook to grab they dynamic parts of the path (:itemID).
14
- const item = { }
25
+ if ( ! items . length ) return "SUCKER I WON!" ;
26
+ const item = items . find ( item => item . id === parseInt ( itemID ) ) ;
27
+ // 1 === "1" => false
28
+ // NEVER EVER EVER EVER EVER USE DOUBLE EQUALS!
15
29
16
30
return (
17
31
< div className = 'item-wrapper' >
@@ -27,12 +41,22 @@ export default function Item(props) {
27
41
28
42
< nav className = 'item-sub-nav' >
29
43
{ /* 👉 STEP 8 - Here go the NavLinks to `<current url>/shipping` and `<current url>/description` */ }
44
+ < NavLink to = { `${ url } /description` } > Description</ NavLink >
45
+ < NavLink to = { `${ url } /shipping` } > Shipping</ NavLink >
30
46
</ nav >
31
47
32
48
{ /* 👉 STEP 9 - Here go the Routes for `<current path>/shipping` and `<current path>/description` */ }
33
49
{ /* These Routes should render <ItemDetails /> */ }
34
50
35
51
{ /* 👉 STEP 10 - Shorten paths and urls with `useRouteMatch` hook */ }
52
+ < Switch >
53
+ < Route path = { `${ path } /description` } >
54
+ < ItemDetails text = { item . description } />
55
+ </ Route >
56
+ < Route path = { `${ path } /shipping` } >
57
+ < ItemDetails text = { item . shipping } />
58
+ </ Route >
59
+ </ Switch >
36
60
</ div >
37
61
)
38
62
}
0 commit comments