Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

119
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda