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 in Node-js by Ashutosh
• 20,830 points
75 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 by Kavya

Related Questions In Node-js

0 votes
1 answer

How do you implement breadcrumbs in a React-Router app?

Breadcrumbs help users navigate by showing the ...READ MORE

answered Feb 24 in Node-js by Kavya
70 views
0 votes
1 answer

How do you handle scroll restoration in a React-Router app?

By default, React Router does not restore ...READ MORE

answered Feb 24 in Node-js by Kavya
83 views
0 votes
1 answer

How do you implement routing in a React application?

Implementing Routing in a React Application (React ...READ MORE

answered Feb 23 in Node-js by Kavya
68 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
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 in Node-js by anonymous
84 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