Part 15 / Topic 3 of 9
useParams: reading a dynamic segment from the URL
A path segment starting with : is a dynamic segment - whatever the real URL has there gets parsed out and handed back through useParams(). examples/03_dynamic_params.jsx:
// examples/03_dynamic_params.jsx
<Route path="/orders/:orderId" element={<OrderDetail />} />
function OrderDetail() {
const { orderId } = useParams();
const order = ORDERS[orderId];
// ...
}
orderId is never passed to OrderDetail as a prop anywhere in this file - landing directly on /orders/5022 really shows "Order 5022: Meghana Foods, ₹650" with no click involved at all, and clicking a different order's link from the list really re-reads a different orderId off the new URL (examples/03_dynamic_params.test.jsx). An id with no matching order really shows the not-found branch instead of crashing on undefined.restaurant - OrderDetail checks order exists before reading anything off it, the same defensive habit Part 4's list rendering already taught for "is this empty," just for "does this id exist" instead.
Done here? The practice set for Part 15 — including the solutions — is on the part page.