Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to ignore last url part with react router?

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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;

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading