Dynamic Routing in React allows us to navigate between different pages and update content dynamically based on URL parameters. This is essential for creating interactive and user-friendly web applications.
In this episode, we explored how to implement dynamic routing using React Router DOM, fetch and display data dynamically, and improve user experience with Shimmer UI (Skeleton Loader).
Unlike static routing where all routes are predefined, dynamic routing allows us to render content dynamically based on URL parameters. This is particularly useful for pages like product details, user profiles, and blog posts, etc.
For example, in an e-commerce website:
/product/1
→ Displays details of Product 1/product/2
→ Displays details of Product 2
We achieve this using react-router-dom
and useParams()
.
To enable routing in React, install the required package:
npm install react-router-dom
We use createBrowserRouter
and RouterProvider
to define routes. The main routes include:
/
→ Displays all products/product/:productId
→ Displays specific product details/men
,/women
,/kids
→ Category pages/cart
→ Shopping cart
The Outlet
component is used to render nested routes inside a parent layout containing Navbar and Footer.
To dynamically fetch product details based on the productId
, we:
- Use
useParams()
to extractproductId
from the URL. - Fetch product details using
useEffect()
when the component mounts. - Display product details or show a Shimmer UI while fetching.
import { useEffect, useState } from "react";
import { useParams } from "react-router-dom";
import Skeleton from "../components/Skeleton";
export const ProductDetails = () => {
const [singleProduct, setSingleProduct] = useState(null);
const { productId } = useParams();
useEffect(() => {
fetch(`https://fakestoreapi.com/products/${productId}`)
.then((res) => res.json())
.then((data) => setSingleProduct(data));
}, []);
return singleProduct === null ? (
<Skeleton />
) : (
<div>
<img src={singleProduct.image} alt={singleProduct.title} />
<h1>{singleProduct.title}</h1>
<p>Price: ${singleProduct.price}</p>
<p>{singleProduct.description}</p>
</div>
);
};
We use <Link>
instead of <a>
to navigate without refreshing the page.
Example:
<Link to={`/product/${product.id}`}>
<Product product={product} />
</Link>
This ensures smooth navigation and maintains state between page transitions.
While fetching data, instead of showing a blank screen, we display a Skeleton Loader to improve the user experience.
if (singleProduct === null) {
return <Skeleton />;
}
This prevents layout shifts and provides a smoother experience while waiting for API data.
✅ Dynamic Routing renders content dynamically based on URL parameters. ✅ useParams() extracts route parameters. ✅ useEffect() fetches dynamic data when the component mounts. ✅ Shimmer UI enhances user experience during API fetch. ✅ enables seamless navigation between pages.