-
Notifications
You must be signed in to change notification settings - Fork 60
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
Bug with anchors #37
Comments
Note: maybe that not the fault of react-router-scroll since we are catching links in the page body, but we just update the browser history so react-router can handle the updates. |
If you're handling the link yourself, use Eventually there should be support for hash links here, but there isn't yet. |
In our case, we are not especially "handling the link ourselves". We are letting react-router change the component if relevant. const shouldUpdateScroll = (prevProps, props)=> {
return !prevProps || prevProps.location.pathname !== props.location.pathname
} (no need to test the origin, hostname etc as if it's on the app runtime it means it is already the same origin etc.) What do you think about this simple method? |
That wouldn't work for e.g. |
Good catch haha. Will provide a better simple default then. |
What I mean is – if you're using hash links, you can set something up... I'm not sure that it's generic enough that I want to pull it in here. Edge cases and edge cases, you know. |
Oh, I see. Maybe we can add something to the readme then? |
Sure. Care to PR a note |
@MoOx Thanks for opening that thread and sharing your solution. function shouldUpdateScroll(prevProps, props) {
const hash = props.location.hash;
// We have no hash to take into account.
// We let react-router handling it.
if (hash === '') {
return true;
}
// It's our first navigation.
if (!prevProps) {
// Push onto callback queue so it runs after the DOM is updated,
// this is required when navigating from a different page so that
// the element is rendered on the page before trying to getElementById.
setTimeout(() => {
const element = document.querySelector(hash);
if (element) {
element.scrollIntoView();
}
}, 0);
}
return false;
} |
btw this is natively supported now since https://github.com/taion/scroll-behavior/releases/tag/v0.9.3 |
@taion I confirm, thanks for raising that point 😄 ! (We were using v0.9.2) |
Each page view I am scrolling to the top of the page, to prevent showing the new page halfway. This conflicts with anchor links that obviously should not scroll the page. Somehow only Edge has a problem with this situation. So I am using a solution (simpler than mentioned above) that works for Edge and other browsers: const maybeScrollToTop = () => {
const hash = window.location.hash;
if (!hash) {
window.scrollTo(0, 0);
}
};
export const Routes = () => (
<Router onUpdate={maybeScrollToTop} history={browserHistory}>
<Route>
// ...
</Route>
</Router>
); |
Simple to reproduce
You can reproduce locally by doing
The doc will start & open locally.
Tell me if you need more (I made this issue really quickly as I am busy, sorry).
The text was updated successfully, but these errors were encountered: