I have a component called Product which gets prop called product which is an object containing attributes such as id, slug, name etc.
function Product({ product }) {
const productLink = `/products/${product.slug}`;
return (
<Link to={productLink} ></Link>
)
}
Here, I am linking to ProductScreen component using the slug field.
This is the Route i have <Route path='/products/:slug' element={<ProductScreen />} />
I have ProductScreen component like this
function ProductScreen() {
const { slug } = useParams();
return (...)
}
In ProductScreen, i want to get id also because i want to make an axios call like {{baseUrl}}/products/id but all i have is slug here.
In Link component, i know i can pass id as a state prop like <Link to={productLink} state={{id:id}} ></Link> but this is not the ideal solution i feel. Is there any way i can pass id from Link in Product to ProductScreen.