sdfsdf
sdfsdf

Reputation: 5600

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?

Upvotes: 3

Views: 247

Answers (1)

John Matthew
John Matthew

Reputation: 81

<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

Upvotes: 4

Related Questions