New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Link method which prevent same locations in the history #570
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, there's a createLocation
method, why are you not using that one?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it looks good, but I have some questions that might improve the overall PR a little.
Is there a workaround for this issue? It seems like this has been an issue for the last ~3 years. |
@TrentWDB I did not have much time lately. In max 1week I hope to review it ;) |
What's the status on this PR? |
Waiting for the repo owner @mjackson to merge this, after that a new PR for react-router can be made. |
I am also looking for this fix. What is the status? |
@mjackson This type of fix seems to have been discussed ad nauseam for an incredibly long time; even a couple of PRs have vanished. Would be great to get this fixed, finally. |
Hoping to bring attention to this. I'm also awaiting this fix. |
me too!!! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for submitting this PR!
@dabit1 any updates? Should someone else fix the suggested changes and recommit? |
Please can we have this PR updated @dabit1 |
Hello. Struggled with the same problem of react-router and react-router-dom Link's being PUSHing history even if i was on the same /route already. What worked for me was:
Hope is helps |
I solved this problem by checking 'match' instead and rendering an empty a tag vs NavLink accordingly: {match && match.isExact
? <a className={clsNavLink}>{content}</a>
: <NavLink to={url} className={clsNavLink}>
{content}
</NavLink>} |
Hey, just wanted to say that this fix is important for me. I run into this issue when trying to render a component via a route when that component has a dynamically generated state. Thanks!!! |
Hi @mjackson! I did some changes in order to have it ready to be merged. A lot of people is requesting it so if you agree I think it's a good improvment. |
Hey @dabit1, sorry for the delay here. Are you using React Router? If so, would you be open to fixing it there instead of here? This library is designed to be a minimal abstraction over the browser's built-in history API. Someday we hopefully won't need it anymore. That's the only reason I'm hesitant to add more API that the browser has no equivalent for. Having said that, I'm ready to move on this now. This bug is super annoying and has been here way too long. So whether we fix it here or in the router, it'll be fixed in our next release. |
Hi @mjackson ! Nice to meet you finally hehe. So yes, I'm using React Router. But the thing is that in some cases I prefer or I have to use the method Anyway I understand your point since this package is not only to be used for React. Maybe makes more sense if |
@mjackson I'm not sure that implementing this in ReactRouter only is a great idea. We've discussed this before in #470 (comment) |
@mjackson hello, when this feature will be ready to use? |
@mjackson Hello , is there any update on when will this feature be published and ready to be used ? |
ETA please. |
@cocacrave close to never since this PR is more than 1 year old. The long-standing issue will be 2 years soon. 👏 |
Anybody have a workaround you recommend and use? |
Somewhere I found this and it works fine: let lastLocation = null;
history.listen(location => {
lastLocation = location
});
const prevHistoryPush = history.push;
history.push = (pathname, state = {}) => {
if (lastLocation === null || pathname !== lastLocation.pathname + lastLocation.search + lastLocation.hash ||
JSON.stringify(state) !== JSON.stringify(lastLocation.state)) {
prevHistoryPush(pathname, state);
}
}; |
@tomasklim Thanks! |
Hey @mjackson, If you don't have the time or drive to maintain this package, that's fine. But might I suggest to let a team of contributors (that are willing to make sure this project stays alive) take care of it then? You could still be the owner, but others could help you out and implement the vision for the project. To shorten the time needed to get a PR merged, or get answers on issues. |
Here's another workaround I used a while ago to get around this (in my case, a page transition was called when navigating to the same page). Warning, it's hacky: const getUserConfirmation = (location, callback) => {
const [newUrl, action] = location.split('|');
// Check if user wants to navigate to the same page.
// If so, we don't want to trigger the page transitions.
// We have to check the `action`, because the pathnames
// are the same when going back in history 🤷
const currentUrl = `${window.location.pathname}${window.location.search}`;
if (newUrl === currentUrl && action === 'PUSH') {
callback(false);
return;
}
callback(true);
};
// ...
let history = createHistory({ getUserConfirmation });
// `block` must return a string to conform.
// We send both the pathname and action to `getUserConfirmation`.
history.block((location, action) => {
return `${location.pathname}${location.search}|${action}`;
}); |
@guidobouman there is now a roadmap in issues for v4 and 5. However, this issue is not in the list, so I guess we are left with dirty hacks forever. 🤷♂️ |
For all watchers, there is another try now - #689 (comment) 🚀 |
It's little bit annoying that I worked on it and then it's closed with no clear reason. |
Hey everyone, I just realized that I never commented on this issue when I closed it. Sorry about that! For anyone who is wondering, I left an explanation in another issue about why I decided to leave this functionality out of the history library. It basically boils down to this: However, the original concern is totally legit. This is why React Router's This way, history + React Router have parity with the DOM. Our Again, my apologies for not following up here earlier! I definitely should have. |
I created a method that call
push
when current location and next location is different and callreplace
when they are the same.It fixes a known problem which was reported for many users.
#470
#558
#507