Skip to content

Commit 52654aa

Browse files
committed
project finished
1 parent e8bd864 commit 52654aa

File tree

5 files changed

+28
-8
lines changed

5 files changed

+28
-8
lines changed

src/components/App.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { useState, useEffect } from 'react'
22
// 👉 STEP 2 - React Router imports (Routes, Route and Link)
3+
import { Routes, Route, Link } from 'react-router-dom'
34

45
// Components used for the different routes
56
import Home from './Home'
@@ -27,14 +28,20 @@ export default function App(props) {
2728
<h1 className='store-header'>Emily&apos;s Trinkets</h1>
2829
<div className='nav-links'>
2930
{/* 👉 STEP 3 - Make Links to navigate us Home (`/`) and Shop (`items-list`) */}
31+
<Link to='/'>Home</Link>
32+
<Link to='items-list'>Shop</Link>
3033
</div>
3134
</nav>
3235

3336
{/* 👉 STEP 4 - Build Routes, and a Route for each of the components imported at the top */}
3437
{/* Note that the components will need some props in order to work */}
3538
{/* Note that the path that renders Item has a URL parameter */}
3639
{/* Note that the path that renders Item must support nested routes */}
37-
40+
<Routes>
41+
<Route path='/' element={<Home />} />
42+
<Route path='items-list' element={<ItemsList items={stock}/>} />
43+
<Route path='items-list/:itemID/*' element={<Item items={stock}/>} />
44+
</Routes>
3845
</div>
3946
)
4047
}

src/components/Home.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
// We'll need React Router's hook to navigate around
2+
import { useNavigate } from "react-router-dom"
23

34
export default function Home() {
4-
5+
const navigate = useNavigate()
56
// 👉 STEP 5 - Build a click handler that will imperatively navigate us to /items-list
67
const routeToShop = () => {
7-
8+
navigate('items-list')
89
}
910

1011
return (

src/components/Item.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
import React from 'react'
22
// We'll need quite a few imports from react-router-dom
3-
3+
import { useParams, NavLink, Routes, Route } from 'react-router-dom'
44
import ItemDetails from './ItemDetails'
55

66
export default function Item(props) {
77
// We get ALL items through props. We'll use the URL to find out which item is the one to show.
88
const { items } = props
9-
9+
const { itemID } = useParams()
1010
// 👉 STEP 7 - We need to pull item from items, using a parameter in the URL (:itemID)
1111
// Beware! The ids are integers, whereas URL parameters are strings.
1212
// Beware! The JSX is expecting 'item' to exist instantly!
1313
// we need useParams to grab the dynamic parts of the path (:itemID).
14+
1415

15-
const item = {}
16+
17+
const item =items.find(it => it.id == itemID)
1618

1719
// This guards against a crash (the data is not available instantaneously)
1820
if (!items.length) return 'Getting your item...'
@@ -31,12 +33,17 @@ export default function Item(props) {
3133

3234
<nav className='item-sub-nav'>
3335
{/* 👉 STEP 8 - Here go the NavLinks to `<current url>/shipping` and `<current url>/description` */}
36+
<NavLink to='description'>Description</NavLink>
37+
<NavLink to='shipping'>Shipping</NavLink>
3438

3539
</nav>
3640

3741
{/* 👉 STEP 9 - Here go the Routes for `<current path>/shipping` and `<current path>/description` */}
3842
{/* These Routes should render <ItemDetails /> */}
39-
43+
<Routes>
44+
<Route path='shipping' element={<ItemDetails text='shipping' />} />
45+
<Route path='description' element={<ItemDetails text='description' />} />
46+
</Routes>
4047
</div>
4148
)
4249
}

src/components/ItemsList.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React from 'react'
22
// We'll need a Link from 'react-router-dom'
3+
import { Link } from 'react-router-dom'
34

45
export default function ItemsList(props) {
56
const { items } = props
@@ -12,6 +13,7 @@ export default function ItemsList(props) {
1213
key={item.id}
1314
>
1415
{/* 👉 STEP 6 - Link starts, navigates us from <current url> to <current url>/<id of the item> */}
16+
<Link to={`${item.id}`}>
1517
<>
1618
<img
1719
className='items-list-image'
@@ -21,7 +23,7 @@ export default function ItemsList(props) {
2123
<p>{item.name}</p>
2224
</>
2325
{/* Link ends */}
24-
26+
</Link>
2527
<p>${item.price}</p>
2628
</div>
2729
))}

src/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@ import App from './components/App'
88
import './styles.css'
99

1010
// 👉 STEP 1 - Import Router and wrap the app
11+
import { BrowserRouter } from 'react-router-dom';
1112

1213
const root = ReactDOM.createRoot(document.getElementById('root'));
1314

1415
root.render(
16+
<BrowserRouter>
1517
<App />
18+
</BrowserRouter>
1619
);

0 commit comments

Comments
 (0)