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

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

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 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