Goal: Retreive Id from URL
I have a feed of products with the following code:
<Router>
<Header />
<Switch>
<Route exact path="/" component={Feed} />
<Route exact path="/" />
<Route
exact
path="/product/:productId"
component={DetailProductPage}
/>
</Switch>
</Router>
Here is my feed code:
<div className="feed">
<div className="feed__cards">
{products.map((product) => (
<ProductCard
image={product.image}
brand={product.brand}
name={product.name}
id={product.id}
/>
))}
</div>
</div>
And the ProductCard component that creates the URL:
<div className="productCard__container">
<img src={image} />
<h1>{brand}</h1>
<h2>{name}</h2>
<p>{id}</p>
<p>{<a href={`/product/${id}`}>View</a>}</p>
</div>
When I click on an individual product, I'm redirected to the individual product page:
function DetailProductPage({ id }) {
const productId = { id };
return (
<div>
<h1>Product page</h1>
<p>{id}</p>
</div>
);
}
When I go this route, I get a URL with the product parameter: (e.g., http://localhost:3000/product/6CeJGSm7dBTABs8rRBJh)
How do I query for the 6CeJGSm7dBTABs8rRBJh part of the URL and put it into a variable?