MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
Dynamic URL Parameters with useParams0%
Study Materials
React.js for IntermediateProgress 0%
1.1Component Reusability and Polymorphic Components1.2Render Props Pattern in Modern React1.3Higher-Order Components vs Modern Hooks1.4Compound Components Pattern Explained
4.1Client-Side Routing Architecture with React Router4.2Defining Routes, Route Layouts, and Outlets4.3Dynamic URL Parameters with useParams4.4Programmatic Navigation and useSearchParams
HomeTutorialsReact.js for IntermediateDynamic URL Parameters with useParams
Programmatic Navigation and useSearchParams

Dynamic URL Parameters with useParams

Beginner12 min read•Updated: 2026-11-18•By Er. Sumit Kumar (MSK Institute)
Study MaterialsCheatsheet

1. The Need for Dynamic Route Segments

In modern web applications, you cannot hardcode a separate route for every piece of content in your database:

  • /courses/react-for-beginners
  • /courses/django-backend-architecture
  • /courses/data-analysis-mastery

Creating hundreds of static route tags (<Route path="/courses/course-1" />, <Route path="/courses/course-2" />) is impossible.

Instead, routers use Dynamic URL Parameters (denoted with a leading colon :):

1/courses/:courseSlug

The :courseSlug segment acts as a wildcard variable that captures whatever string occupies that position in the active URL.

2. Defining Dynamic Routes

In your router definition, place a colon : before the parameter name:

1<Routes>
2 <Route path="/" element={<Home />} />
3 <Route path="/courses" element={<CourseCatalog />} />
4
5 {/* Dynamic parameter route */}
6 <Route path="/courses/:courseSlug" element={<CourseDetail />} />
7 
8 {/* Multiple dynamic parameters */}
9 <Route path="/courses/:courseSlug/lessons/:lessonId" element={<LessonViewer />} />
10</Routes>

3. Reading Parameters with the useParams Hook

React Router provides the useParams() hook to extract active URL parameters from inside the rendered component.

useParams() returns an object where keys match the param names defined in your route (:courseSlug -> params.courseSlug):

1import React, { useState, useEffect } from 'react';
2import { useParams, Link } from 'react-router-dom';
3 
4export default function CourseDetail() {
5 // Extract dynamic segment from URL
6 const { courseSlug } = useParams();
7 
8 const [course, setCourse] = useState(null);
9 const [loading, setLoading] = useState(true);
10 
11 useEffect(() => {
12 async function fetchCourseDetails() {
13 setLoading(true);
14 try {
15 const response = await fetch(`/api/courses/${courseSlug}`);
16 const data = await response.json();
17 setCourse(data);
18 } catch (err) {
19 console.error('Failed to load course:', err);
20 } finally {
21 setLoading(false);
22 }
23 }
24 
25 fetchCourseDetails();
26 }, [courseSlug]); // Re-fetch whenever the URL parameter changes!
27 
28 if (loading) return <div className="spinner">Loading syllabus...</div>;
29 if (!course) return <p>Course not found.</p>;
30 
31 return (
32 <div className="course-detail-view">
33 <Link to="/courses" className="back-link">← Back to Catalog</Link>
34
35 <h2>{course.title}</h2>
36 <p className="description">{course.description}</p>
37
38 <h3>Curriculum Modules</h3>
39 <ul>
40 {course.modules.map((m) => (
41 <li key={m.id}>
42 <Link to={`/courses/${courseSlug}/lessons/${m.slug}`}>
43 {m.title}
44 </Link>
45 </li>
46 ))}
47 </ul>
48 </div>
49 );
50}

4. Crucial Hook Rule: Dependency on useParams

Notice line 29: [courseSlug] is included in the useEffect dependency array.

If a user navigates directly from /courses/react-for-beginners to /courses/django-backend, the CourseDetail component does not unmount and remount; only its URL parameter changes! Listing courseSlug in the dependency array guarantees that your data-fetching effect fires again to load the new course data.


Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
How do you declare a dynamic URL parameter in a React Router <Route> path?
Question 2
What does the useParams() hook return?
Question 3
If the route is /users/:userId/posts/:postId and the active URL is /users/42/posts/108, what will useParams() return?
Question 4
Why must the extracted parameter (e.g. courseSlug) be included in the useEffect dependency array?
Question 5
What data type are the values returned in the useParams object?
Next Lesson

Programmatic Navigation and useSearchParams

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
Defining Routes, Route Layouts, and OutletsProgrammatic Navigation and useSearchParams

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

Mentor-Led Practical Training

Master React.js for Intermediate with Hands-On Projects

Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext