preact-router: Problems with link anchors
Whenever I click on a anchor link (for example ‘href=“#123”’), this
addEventListener('popstate', function () {
routeTo(getCurrentUrl());
});
fires and reloads the according route component, what some-have screws up the anchor and anyways is redundant.
About this issue
- Original URL
- State: open
- Created 6 years ago
- Comments: 15 (4 by maintainers)
Thanks for input @developit! But the
nativeattribute only prevents the click handler to run and theroute()andsetUrl()to be called. It doesn’t preventrouteTo()to be called from thepopstatehandler. Which in turn leads toinstance.setState()andinstance.forceUpdate(). Is there a way to prevent this?Hmm - no need for custom code here really - you can use the
nativeprop to bypass preact-router for any link, including anchors. It’ll just use the browser’s default behavior:Before the fix is made in a PR, here is what I did to work around it - the core is DOM manipulation, not tied to any library. Hope it helps. But if you have shadow DOM with anchor, you need to write a custom query selector to walk the available shadow roots of the entire DOM to find the element.
Add the following manual scroll to any page.
@developit We are battle testing a fix in our fork. If successful, I’ll open a PR. I added the following line as the first line in the Router’s
routeTo-method: