Skip to content

Commit a58fcbc

Browse files
committed
Through to Item.js Component
1 parent 6791ce8 commit a58fcbc

File tree

3 files changed

+34
-10
lines changed

3 files changed

+34
-10
lines changed

src/components/App.js

+21-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React, { useState, useEffect } from 'react'
22
// 👉 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";
44
// Link aka ahref
55

66
// Components used for the different routes
@@ -33,8 +33,27 @@ export default function App(props) {
3333
{/* 👉 STEP 3 - Make Links to navigate us Home (`/`) and Shop (`/items-list`) */}
3434
</div>
3535
</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>
3747
{/* 👉 STEP 4 - Build a Switch with a Route for each of the components imported at the top */}
48+
3849
</div>
3950
)
4051
}
52+
53+
/**
54+
* switch (varName) {
55+
* case: "blah":
56+
* doSomething();
57+
* break;
58+
* }
59+
*/

src/components/Home.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import React from 'react'
22
// We'll need React Router's own version of the History API
3+
import { useHistory } from "react-router-dom";
34

45
export default function Home() {
56
// 👉 STEP 5 - Build a click handler that will imperatively
67
// navigate us to <website base URL>/items-list
8+
const history = useHistory();
79

810
const routeToShop = () => {
9-
11+
history.push("/items-list");
1012
}
1113

1214
return (

src/components/ItemsList.js

+10-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import React from 'react'
22
// We'll need a Link and the useRouteMatch hook from 'react-router-dom'
3+
import { Link, useRouteMatch } from "react-router-dom";
34

45
export default function ItemsList(props) {
56
const { items } = props
67

78
// We'll grab the current URL using the hook
9+
const { url } = useRouteMatch();
810

911
return (
1012
<div className='items-list-wrapper'>
@@ -14,14 +16,15 @@ export default function ItemsList(props) {
1416
key={item.id}
1517
>
1618
{/* 👉 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>
2326
{/* Link ends */}
24-
27+
</Link>
2528
<p>${item.price}</p>
2629
</div>
2730
))}

0 commit comments

Comments
 (0)