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

162
Vistas
Get React Router URL

I am trying to write a function that sets the current location to the route that was just clicked. However, as you can see, it gives me the url of the page I was on WHEN I clicked the button, not the page of path of the button itself. What should I use instead of window.location.pathname?

 const setLocation = () => {
    console.log(window.location.pathname);
    setCurrentLocation(window.location.pathname);
  };

 <Link onClick={() => setLocation()} to="/today" className="btn btn-primary btn-sm">

    <Link onClick={() => setLocation()} to="/upcoming" className="btn btn-primary btn-sm">

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Why are you storing this to the state ?

ReactRouter has a hook useLocation https://v5.reactrouter.com/web/api/Hooks/uselocation in which you can take .pathname of any pushed path to history.

If you really want to store location by yourself pass on the route value in to as a param in setLocation function or create a component that wraps the Link component and does store location : )

ie:

<Link onClick={() => setLocation("/upcoming")} to="/upcoming" className="btn btn-primary btn-sm">
about 4 years ago · Juan Pablo Isaza Denunciar

0

code is like ===>

onClick={
(e)=>console.log(e.target.href)
setLocation(e.target.href)
}
const setLocation = (path) => {
    console.log(window.location.pathname);
    setCurrentLocation(window.location.pathname+path);
  };
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