Skip to content

Commit 076ecb8

Browse files
committed
Final solution in place.
1 parent a58fcbc commit 076ecb8

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

src/components/App.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export default function App(props) {
3535
</nav>
3636
<Switch>
3737
<Route path={"/items-list/:itemID"}>
38-
<Item />
38+
<Item items={stock} />
3939
</Route>
4040
<Route path="/items-list">
4141
<ItemsList items={stock}/>
@@ -45,7 +45,6 @@ export default function App(props) {
4545
</Route>
4646
</Switch>
4747
{/* 👉 STEP 4 - Build a Switch with a Route for each of the components imported at the top */}
48-
4948
</div>
5049
)
5150
}

src/components/Item.js

+25-1
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,31 @@
11
import React from 'react'
22
// 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";
310

411
import ItemDetails from './ItemDetails'
512

613
export default function Item(props) {
714
// We get ALL items through props. We'll use the URL to find out which item is the one to show.
815
const { items } = props
916

17+
const { itemID } = useParams();
18+
const { url, path } = useRouteMatch();
19+
console.log(url, path);
20+
1021
// 👉 STEP 7 - We need to pull item from items, using a parameter in the URL (:itemID)
1122
// Beware! The ids are integers, whereas URL parameters are strings.
1223
// Beware! The JSX is expecting 'item' to exist instantly!
1324
// 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!
1529

1630
return (
1731
<div className='item-wrapper'>
@@ -27,12 +41,22 @@ export default function Item(props) {
2741

2842
<nav className='item-sub-nav'>
2943
{/* 👉 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>
3046
</nav>
3147

3248
{/* 👉 STEP 9 - Here go the Routes for `<current path>/shipping` and `<current path>/description` */}
3349
{/* These Routes should render <ItemDetails /> */}
3450

3551
{/* 👉 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>
3660
</div>
3761
)
3862
}

0 commit comments

Comments
 (0)