Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

118
Visualizações
Regex to use params in path

How to create a path that can handle zero, one, the other or both parameters at once (react router v5).

For example:

/offers /offers/q-shoes /offers/London /offers/London/q-shoes

I am trying to achieve it this way, unfortunately in this case the path /offers/London and /offers is not captured

import React from 'react';
import pathToRegexp from 'path-to-regexp';
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';

import { Home, Browse } from './Pages';

const re = pathToRegexp('/offers/:location?/q-:search?');

const App = () => (
  <Router>
    <Switch>
      <Route path={re}>
        <Browse />
      </Route>
     
      <Route path="/">
        <Home />
      </Route>
    </Switch>
  </Router>
);

export default App;
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

According to documentation you can use string or array of strings as path.

pathToRegexp function returning a RegEx object so you cannot use it. Instead, you should use an array of strings, with the order being more specific to less specific.

import React from "react";
import {
  BrowserRouter as Router,
  Route,
  Switch,
  Link,
  withRouter
} from "react-router-dom";

const Component = withRouter((props) => {
  return (
    <div>
      <div>{props.title}</div>
      <div>{JSON.stringify(props.match.params)}</div>
    </div>
  );
});

const Home = () => <Component title="Home" />;
const Browse = () => <Component title="Browse" />;
const NoMatch = () => <Component title="NoMatch" />;

const pathArray = ["/offers/:location?/q-:search?", "/offers/:location?"];

const App = () => (
  <Router>
    <div style={{ display: "flex", flexDirection: "column" }}>
      <Link to="/">/Home</Link>
      <Link to="/offers">/offers</Link>
      <Link to="/offers/q-shoes">/offers/q-shoes</Link>
      <Link to="/offers/London">/offers/London</Link>
      <Link to="/offers/London/q-shoes">/offers/London/q-shoes</Link>
    </div>
    <Switch>
      <Route path={pathArray}>
        <Browse />
      </Route>

      <Route path="/">
        <Home />
      </Route>
      <Route>
        <NoMatch />
      </Route>
    </Switch>
  </Router>
);

export default App;
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda