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

259
Visualizações
ReactJS - Add slug to url (dynamic slug in frontend)

I'm trying to create something like here on stackoverflow:

When I enter this URL:

https://stackoverflow.com/questions/70615440

it will update the url to:

https://stackoverflow.com/questions/70615440/refactor-switch-statement-to-reduce-code-complexity

The url is possible to visit by id. It will add / update the slug to the url. I can also use the a random slug https://stackoverflow.com/questions/70615440/xxx and it will update it to the correct one :https://stackoverflow.com/questions/70615440/refactor-switch-statement-to-reduce-code-complexity

xenForo does something similar:

https://xenforo.com/community/threads/200874/
https://xenforo.com/community/threads/xenforo-2-2-8-patch-1-and-add-on-updates-released.200874/

How can I achieve something like this in a React app? How is that behavior called?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

It's called routing. You can install react-router-dom and use useHistory and useLocation hooks. The usage is very simple. From documentation about useLocation:

The useLocation hook returns the location object that represents the current URL.

You can save the current URL in pathname and compare it with the id, if they match, you can use useHistory for navigation.

Read more

Example code:

const { pathname } = useLocation();
const history = useHistory();
const handleChange = () => history.push("/questions/70615440/refactor-switch-statement-to-reduce-code-complexity");

useEffect(() => {
    if(pathname === "/questions/70615440"){handleChange()}
  }, []);
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