Skip to content
Advertisement

Trying to add a detail product component. React-router

I have a component products which lists every product in the /Productsurl. I’m trying to make another component Detail that displays specific product in the browser. This is the URL I’ve been trying to create Detail/{some id goes here}. The problem is how am i going to know which product clicked, so how am I supposed to show this specific product in the Detail component. I do have a Detail and Productscomponent.

My router is below:

JavaScript

I’m not going to share my Productscomponent since It’s composed 200 lines of code. I’m just gonna share the search svg in my Products that I want it to take me to the Detail/{id}page when I click.

JavaScript

Advertisement

Answer

This is what you will need – https://reactrouter.com/web/example/url-params/

In summary, the :int can be availed in the Detail component as const {int} = useParams();. And while handling the click in the Products component, you will have to pass the product-id as /Details/{productId} using the Link component.

In this manner once the Link is clicked to bring the route to Detail/xyz-product-id, the Detail component will render having that xyz-product-id available as int in the code.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement