Search code examples
reactjsroutesrestful-url

In React, can I use "useParams" to access RESTful components of my URL?


I'm using React 16.13.0. Within my component, I can access query string params (e.g. /url?coop_id=23) like so

import { useParams } from "react-router-dom";
...
const { coop_id } = useParams();

However, how do I access the params in a RESTful format? E.g. if my URL is /edit/23/home, what's the React way to get the "23"? I have defined the route in my src/App.js file

          <Switch>
            ...
            <Route path="/edit/:id/home" component={Edit} />

if that's useful.


Solution

  • import { useParams} from "react-router";
    import { BrowserRouter as Router, Route, Link } from "react-router-dom";
    
    const Portfolio = () => {
     let { id } = useParams();
     return (
        <div>
            Portfolio component
            <p>Topic: {id}</p>
        </div>
            );
     };