Home > Back-end >  How to ignore last url part with react router?
How to ignore last url part with react router?

Time:04-11

I want to use the react router to show a page according to the first part of the URL, because the last part of the URL is holding an ID. How can I read out the last part, now I'm using string.split("/").pop()

URL http://localhost:3003/profile/313a2333

Router

<Routes>
  <Route path="/profile" element={<Profile/>} />
</Routes>

CodePudding user response:

Define your Route like the following:


<Route path="/profile/:user_id">

Then you can get 2nd parameter in Profile component.

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


const params: any = useParams();
const user_id = params.user_id;

  • Related