Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

118
Views
How to match pattern with Id on them react router

I am new to react. I have a route with react router that takes :id as parameter, how do I match this using react-router-dom useLocation.

basically path can be posts or post/21 how do I match post/21 to render a component, I want to render a seperate component if I am on the detail page

{path === 'post/:id' ? <PostDetail /> : <Posts /> }

but use location returns posts/1 which I can't use to match the post detail

summary Any path that matches: post/:id

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Typically you will use a Switch component with a set of Route components. Each Route takes a path prop that is used for matching.

import React from 'react';
import { BrowserRouter, Switch, Route, useRouteMatch } from 'react-router-dom';

const App = (props) => {
  return (
    <BrowserRouter>
      <Switch>
        <Route path="/post/:id">
          <PostDetail />
        </Route>
        <Route path="/post">
          <Posts />
        </Route>
      </Switch>
    </BrowserRouter>
  );
};

Components that are children of a Route can use the useRouteMatch hook to access path params.

const PostDetail = (props) => {
  const match = useRouteMatch();
  return <h1>{match.params.id}</h1>;
};

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!