Dynamic Routes and Route Params
0 145
๐ What Are Dynamic Routes?
Dynamic routes allow you to build pages with variable paths โ like user profiles, blog posts, or product pages โ where part of the URL is dynamic. Instead of hardcoding each route, React Router lets you use placeholders to handle these cases smoothly. ๐
๐ ๏ธ How to Define a Dynamic Route
You can define a dynamic segment in the route path using a colon (:
), like this:
// App.js
import { Routes, Route } from 'react-router-dom';
import UserProfile from './pages/UserProfile';
function App() {
return (
<Routes>
<Route path="/user/:id" element={<UserProfile />} />
</Routes>
);
}
Here, :id
is a route parameter, which you can access inside the UserProfile
component. This makes it possible to render different content based on the URL. ๐
๐ฅ Accessing Route Parameters with useParams
To get the dynamic value from the URL, use the useParams
hook from react-router-dom
:
import { useParams } from 'react-router-dom';
function UserProfile() {
const { id } = useParams();
return <h2>User ID: {id}</h2>;
}
For example, visiting /user/101
will display: User ID: 101. ๐
๐ Creating Links to Dynamic Routes
To navigate to a dynamic route, use <Link />
or useNavigate
with dynamic values:
import { Link } from 'react-router-dom';
<Link to="/user/123">View User 123</Link>
Or programmatically:
import { useNavigate } from 'react-router-dom';
const navigate = useNavigate();
navigate(`/user/${userId}`);
๐ฆ Practical Example: Blog Posts
Letโs say you have a blog, and you want each post to have a dynamic URL based on its slug
:
// App.js
<Route path="/blog/:slug" element={<BlogPost />} />
// BlogPost.js
import { useParams } from 'react-router-dom';
function BlogPost() {
const { slug } = useParams();
return <h1>Reading: {slug.replace('-', ' ')}</h1>;
}
Now, visiting /blog/react-router-guide
will render: Reading: react router guide. โจ
๐ Nested Dynamic Routes
You can also nest dynamic routes for complex structures:
// App.js
<Route path="/user/:id" element={<UserProfile />}>
<Route path="settings" element={<UserSettings />} />
</Route>
Now, the URL /user/123/settings
will render the UserSettings
component nested inside UserProfile
.
โ Catching Invalid Params
Sometimes users may enter invalid route params. You can validate or redirect them using:
import { Navigate, useParams } from 'react-router-dom';
function ProductPage() {
const { id } = useParams();
if (isNaN(id)) return <Navigate to="/not-found" />;
return <h1>Product ID: {id}</h1>;
}
๐ง Tips for Using Route Params
- ๐งน Always validate or sanitize route params before using them
- ๐ Re-fetch data when param changes using
useEffect
- ๐งฉ Combine route params with query strings if needed
๐ Summary
- โ
Define dynamic paths using
:paramName
- โ
Use
useParams()
to access values inside components - โ
Programmatically navigate with
useNavigate
- โ Combine with nested routes for advanced layouts
- โ Handle invalid params gracefully
๐ Final Thoughts
Dynamic routes and route parameters give your React apps power and flexibility. Whether youโre building user dashboards, product pages, or blog platforms, understanding how to handle dynamic URLs is a must-have skill in any developer's toolkit. ๐๐ก
If youโre passionate about building a successful blogging website, check out this helpful guide at Coding Tag โ How to Start a Successful Blog. It offers practical steps and expert tips to kickstart your blogging journey!
For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!

Share:
Comments
Waiting for your comments