React · Lesson 68 of 120
Links and Params
Product pages.
A shop doesn't write one route per product. Instead, one route has a parameter in its path, and the page reads it to decide what to show.
- One route
- A parameter
- Read it
Product pages
The path has a parameter, colon i d. Use params reads it from the address. The product page loads that product, whenever the id changes. The links build each address with a template string. We open product three. Run it.
App.jsxjsx
import { useState, useEffect } from "react";
import { MemoryRouter, Routes, Route, Link, useParams } from "react-router";
import { getProduct } from "./api";
function ProductPage() {
const { id } = useParams();
const [product, setProduct] = useState(null);
useEffect(() => { getProduct(id).then(setProduct); }, [id]);
return product ? <p className="box">{product.name} — £{product.price}</p> : <p>Loading...</p>;
}
export default function App() {
return (
<MemoryRouter>
{[1, 2, 3].map(n => <Link key={n} id={"p" + n} to={`/products/${n}`}>Product {n} </Link>)}
<Routes>
<Route path="/products/:id" element={<ProductPage />} />
</Routes>
</MemoryRouter>
);
}Output
No routes matched location "/"
Also uses style.css
body{font-family:-apple-system,Helvetica,Arial,sans-serif;margin:22px;font-size:21px;color:#1d1d1f;background:#fafafa}
h1{font-size:34px;margin:0 0 14px} h2{font-size:26px;margin:0 0 10px}
button{font-size:19px;padding:9px 18px;border-radius:9px;border:0;background:#61dafb;color:#111;font-weight:600;margin:4px 6px 4px 0}
input{font-size:19px;padding:8px 10px;border:2px solid #bbb;border-radius:8px}
li{margin:5px 0} .box{padding:14px;border-radius:12px;background:#fff;border:2px solid #ddd;margin:8px 0}
.done{text-decoration:line-through;color:#999} small{color:crimson;display:block;font-size:16px} .big{font-size:64px;font-weight:800}Also uses api.js
const PRODUCTS = [
{ id: 1, name: "Trail 500", price: 849.5, type: "bike", stock: 12 },
{ id: 2, name: "City 200", price: 450, type: "bike", stock: 0 },
{ id: 3, name: "Volt 7", price: 2199, type: "bike", stock: 4 },
{ id: 4, name: "Helmet", price: 40, type: "gear", stock: 30 },
{ id: 5, name: "Lights", price: 25, type: "gear", stock: 18 },
];
const wait = ms => new Promise(resolve => setTimeout(resolve, ms));
export async function getProducts() {
await wait(300);
return PRODUCTS;
}
export async function getProduct(id) {
await wait(300);
const product = PRODUCTS.find(p => p.id === Number(id));
if (!product) throw new Error(`Product ${id} not found`);
return product;
}Also uses actions.json
[{"click": "#p3"}, {"wait": 600}]Navigating in code
Sometimes you navigate after an event, like submitting a form. Use navigate gives you a function for that. Call it with a path, and the router moves there.
| key | value |
|---|---|
| Read params | useParams() |
| Navigate | useNavigate() |
| Call | navigate("/cart") |
| Back | navigate(-1) |
Summary
Put parameters in paths, and read them with use params. Next, nested routes.
Key takeaways- /products/:id
- useParams
- Template links
Next up: Nested Routes — shared layouts