Hackforge Academy

Category: react

routin param in react

Published on 15 Apr 2026

Explanation

React Router allows you to pass parameters in the URL (called route params). These params help load dynamic content like user profiles, course details, or product pages based on an ID or name in the path.

Code:

import { BrowserRouter, Routes, Route } 
from 'react-router-dom';
import User from './User';

function App() {
  return (
    <BrowserRouter>
      <Routes>
        <Route path="/user/:id" 
element={<User />} />
      </Routes>
    </BrowserRouter>
  );
}

Explanation

The ':id' part in the route path is a dynamic parameter. You can access this value inside the component using the useParams() hook.

Code:

import { useParams } from 'react-router-dom';

function User() {
  const { id } = useParams();

  return <h2>User ID: {id}</h2>;
}

export default User;

Explanation

Example URL: If the route is defined as /user/:id and the browser navigates to /user/101, then the id parameter will be 101 inside the component.

Code:

// URL example
http://localhost:3000/user/101

// Output
User ID: 101

Explanation

You can also pass multiple parameters in a single route by defining more dynamic segments.

Code:

<Route path="/course/:courseId/
lesson/:lessonId" element={<Lesson />} 
/>

// Accessing params
const { courseId, lessonId } = useParams();

πŸš€ Learn Spring Boot with real-world projects

πŸ’‘ Build REST APIs step by step

🧠 Improve backend development skills

🎯 Get career-ready practical training

Join Our Free WhatsApp Community

Direct access to niche-specific mentors and peers on WhatsApp.

🐍

Python Community

Discuss Django, FastAPI, AI integration, and automation scripts with 15k+ developers.

Join Python Community
βš›οΈ

React Community

Master Next.js, Framer Motion, and State Management. Share your latest UI components.

Join React Community
β˜•

Java Community

Deep dives into Spring Boot, Microservices architecture, and high-performance backend ops.

Join Java Community