Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

163
Views
Obtener la URL del enrutador React

Estoy tratando de escribir una función que establezca la ubicación actual en la ruta en la que se acaba de hacer clic. Sin embargo, como puede ver, me da la URL de la página en la que estaba CUANDO hice clic en el botón, no la página de la ruta del botón en sí. ¿Qué debo usar en lugar de 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 answers
Answer question

0

¿Por qué le estás guardando esto al estado?

ReactRouter tiene un gancho useLocation https://v5.reactrouter.com/web/api/Hooks/uselocation en el que puede tomar .pathname de cualquier ruta empujada al historial.

Si realmente desea almacenar la ubicación usted mismo, pase el valor to la ruta como un parámetro en la función setLocation o cree un componente que envuelva el componente Link y almacene la ubicación:)

es decir:

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

0

el código es como ===>

 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!