Can you explain the concept of dynamic routing in React Router and provide an example

0 votes
Can you tell me Can you explain the concept of dynamic routing in React Router and provide an example?
4 days ago in Node-js by Ashutosh
• 27,610 points
26 views

1 answer to this question.

0 votes

Dynamic routing allows you to define routes at runtime (e.g., based on API data, user roles, or app state) instead of hardcoding them.

Key Concepts

Route Parameters (:id) → Match dynamic URL segments.

Nested Routes → Load components conditionally.

Data Fetching → Fetch data before rendering (e.g., loader in React Router 6.4+).

Example: Dynamic User Profiles

1. Define Dynamic Routes

// App.jsx

import { Routes, Route } from 'react-router-dom';

import UserProfile from './UserProfile';

function App() {

  return (

    <Routes>

      {/* Static route */}

      <Route path="/" element={<Home />} />

      {/* Dynamic route */}

      <Route path="/users/:userId" element={<UserProfile />} />

    </Routes>

  );

}

2. Access Route Parameters

// UserProfile.jsx

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

export default function UserProfile() {

  const { userId } = useParams(); // Extracts :userId from URL

  return <h1>Profile Page for User: {userId}</h1>;

}

answered 4 days ago by anonymous

Related Questions In Node-js

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

How to manage circular dependencies in Angular services using dependency injection?

Circular dependencies arise when multiple services rely ...READ MORE

answered 4 days ago in Angular by anonymous
37 views
0 votes
1 answer
0 votes
0 answers

How do I add a hyperlink to a tooltip?

Can you tell me How do I ...READ MORE

4 days ago in Node-js by Ashutosh
• 27,610 points
26 views
0 votes
1 answer

What are the main differences between BrowserRouter and HashRouter in React Router?

Here's a precise comparison between BrowserRouter and HashRouter in React Router ...READ MORE

answered 4 days ago in Laravel by anonymous
36 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP