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

146
Visualizações
How to remove trailing forward slash from History before pushing?

I understand that due to some React Router logic, a forward slash is appended onto the url on refresh.

Some context, I am displaying a page that renders some company info based on the company at the end of the url. Since I split and extract the companies via "/", my page loads perfectly with or without the last slash.

The problem now is that if I ever refresh the page, and it appends a trailing forward slash, when I change the company, it messes up.

Example:

Original URL: https://www.payranked.com/companies/bytedance

Refreshed URL: https://www.payranked.com/companies/bytedance/

If I were to navigate to another page:

Without trailing slash: https://www.payranked.com/companies/accenture

With trailing slash: https://www.payranked.com/companies/bytedance/accenture

As seen, if there is a page refresh, then my URL gets messed up.

Here is my current code:

const history = useHistory();

// Some code...

// Used to extract company string, then uses MySQL call to display info
const location = useLocation();
const companyURL = location.pathname.split('/')[2];

// companyDropdown is the key returned from selecting a company in the dropdown.
let companyDropdownURL = CompanyNameToURL[companyDropdown]; 

history.push({
  pathname:  `${companyDropdownURL}`,
    state: {
      response: '' 
    } 
});

companyDropdownURL is essentially a value from a map CompanyNameToURL. I.e. if I was searching JPMorgan Chase (companyDropdown), companyDropdownURL would be jpmorgan-chase, which is reflected in the URL.

How would I feasible approach this issue, either by editing the history somehow or by another method. Thanks!

Edit: Added more information

about 4 years ago · Juan Pablo Isaza
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