How do you pass and use URL parameters in a React-Router route

0 votes
With the help of code can you tell me How do you pass and use URL parameters in a React-Router route?
Feb 22, 2025 in Node-js by Ashutosh
• 33,350 points
523 views

1 answer to this question.

0 votes

Passing and Using URL Parameters in React Router

1. Define a Route with a Parameter

import { BrowserRouter as Router, Route, Routes } from "react-router-dom";

import UserProfile from "./UserProfile";

const App = () => {

  return (

    <Router>

      <Routes>

        <Route path="/user/:id" element={<UserProfile />} />

      </Routes>

    </Router>

  );

};

export default App;

2. Access the Parameter in the Component

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

const UserProfile = () => {

  const { id } = useParams(); // Extracts 'id' from the URL

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

};

export default UserProfile;

3. Navigate with a Dynamic URL

import { Link } from "react-router-dom";

<Link to="/user/123">Go to User 123</Link>

answered Feb 23, 2025 by Kavya

Related Questions In Node-js

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

How do you handle route transitions and animations in React applications using React Router?

To handle route transitions and animations in ...READ MORE

answered Apr 24, 2025 in Node-js by anonymous
768 views
0 votes
1 answer
0 votes
1 answer

How would you implement a chat application using MongoDB’s data model patterns?

To implement a chat application using MongoDB’s ...READ MORE

answered Feb 23, 2025 in Node-js by Kavya
575 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

How do you redirect a user to a different route in React Router?

Redirecting a User in React Router 1. Using ...READ MORE

answered Feb 23, 2025 in Node-js by Kavya
498 views
0 votes
1 answer

What are MongoDB data types, and how do you define them in a schema?

MongoDB supports various data types, including: String (String) ...READ MORE

answered Feb 23, 2025 in Node-js by anonymous
516 views
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