Search⌘ K

Dynamic Routing

Explore how to handle dynamic routing in React applications by defining routes with parameters and using the useParams hook. Understand how to create interactive URLs that change based on user actions and apply these techniques through practical exercises such as adding user profiles and blog posts.

In many real-world applications, URLs often contain dynamic data. For example, a product page URL in an online store might look like /products/123, where 123 is the product ID that changes for each product available in the store.

In React applications, such scenarios are handled through dynamic routing by defining routes with parameters. Dynamic routing enables us to define routes with placeholders for parameters, allowing URLs to include dynamic values. React Router supports dynamic routing by using a colon (:) in the route path to represent a parameter. For example:

<Route path="/products/:id" element={<ProductDetail />} />

:id is a dynamic parameter that can hold any value, such as 123 or abc.

Adding links with product IDs

Before extracting parameters from a URL, let’s create a product page that includes links with dynamic product IDs in the URL.

Understanding the URL structure

Take a closer look at ...