Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you prevent consecutive URLs in React Router?

Let's say you click something like <Link to="/">{...}</Link> twice. You will push the "/" pathname twice onto the history.location. How would you go about preventing the same pathname being pushed onto history.location two times in a row? Is this a bad UX?

like image 582
sdfsdf Avatar asked Jan 20 '26 06:01

sdfsdf


1 Answers

<Link> tags in react-router-dom have a replace boolean parameter. You can check the current pathname if it matches with the incoming pathname.

<Link to="/" replace={location.pathname === "/"}>...</Link>

You can check it out https://reacttraining.com/react-router/web/api/Link/replace-bool

like image 71
John Matthew Avatar answered Jan 22 '26 18:01

John Matthew



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!