How to pass parameters with react-router

0 votes
Can you tell me How to pass parameters with react-router?
Feb 22 in Node-js by Nidhi
• 11,580 points
90 views

1 answer to this question.

0 votes

Passing Parameters with React Router

1. Define a Route with Parameters

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

import ProductDetails from "./ProductDetails";

const App = () => {

  return (

    <Router>

      <Routes>

        <Route path="/product/:id" element={<ProductDetails />} />

      </Routes>

    </Router>

  );

};

export default App;

2. Pass Parameters Using <Link>

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

const ProductList = () => {

  return (

    <div>

      <Link to="/product/101">View Product 101</Link>

      <Link to="/product/202">View Product 202</Link>

    </div>

  );

};

export default ProductList;

3. Access Parameters in the Component Using useParams

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

const ProductDetails = () => {

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

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

};

export default ProductDetails;

answered Feb 23 by Kavya

Related Questions In Node-js

0 votes
1 answer

How to Pass route control with optional parameter after root in express?

Hello @kartik, That would work depending on what ...READ MORE

answered Oct 12, 2020 in Node-js by Niroj
• 82,840 points
14,664 views
0 votes
1 answer

How to pass request query parameters through a Node.js application?

Using Node.js http Module Step 1: Basic Server ...READ MORE

answered Dec 13, 2024 in Node-js by Navya
106 views
0 votes
1 answer
0 votes
1 answer

How to convert component with many states to react functions?

When converting a class component with multiple ...READ MORE

answered Feb 22 in Node-js by Kavya
56 views
0 votes
1 answer

What is the use of Switch in React Router?

In React Router v5, <Switch> is used ...READ MORE

answered Feb 23 in Node-js by Kavya
47 views
0 votes
1 answer

How do you create protected routes in React?

Creating Protected Routes in React (React Router ...READ MORE

answered Feb 23 in Node-js by Kavya
76 views
0 votes
1 answer

What is the difference between BrowserRouter and createBrowserRouter?

Feature BrowserRouter (React Router v5 & v6) createBrowserRouter (React ...READ MORE

answered Feb 23 in Node-js by Kavya
86 views
0 votes
1 answer

How can you preload data for a route in React?

Preloading Data for a Route in React ...READ MORE

answered Feb 24 in Node-js by Kavya
97 views
0 votes
1 answer
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