-
Notifications
You must be signed in to change notification settings - Fork 62
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
Doesn't scroll on reload / refresh #13
Comments
Hi, I'm not able to reproduce this issue. When I go to http://react-router-hash-link.rafrex.com/bar#section-two and refresh in Chrome/Safari/Firefox is scrolls properly (the browser is handling the scroll in this case). Do you have a failing example I can look at? |
Hmm cool. First thanks for responding so quickly.
Let me get an example together
…On Fri, 19 Jan 2018 at 3:58 pm, Rafael Pedicini ***@***.***> wrote:
Hi, I'm not able to reproduce this issue. When I go to
http://react-router-hash-link.rafrex.com/bar#section-two and refresh in
Chrome/Safari/Firefox is scrolls properly (the browser is handling the
scroll in this case).
Do you have a failing example I can look at?
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#13 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AA19YUijTvXzQy3rrtb91rMeb-por2QQks5tMCDvgaJpZM4Rj-Fg>
.
|
Sorry my bad, it is working! Cheers |
@cjke How did you get it to work? Having the same issue. |
Well it's strange - for the longest time it wasn't (I swear it wasn't, as I have been looking for solutions from the start of one particular project which required it). Of recent it just started working. No rhyme, no reason. I don't know if there was a React update, a React Router update, or a Chrome update. All I can say, is do a hard refresh and start from the top. |
scroll works for me. |
In my case content is fetched from API, so when the app loads, content is not there yet and the page remains at the top. Any ideas on how to get it working? |
@yantakus you can use this stackoverflow answer to scroll to a given element. Once your Yes, the solution is not linked to |
@RAFREX, I face this issue as well: If you go to https://7y6r6x2yr0.codesandbox.io/you#go-here from an external source or refresh/reload or enter it manually and press Enter, Chrome does not go to the /cc @cjke |
Same here. |
On a fresh page load the browser handles scrolling to the I have an idea of how to add initial page load scrolling to In the mean time you can you can add initial load scrolling to your App's class App extends React.Component {
componentDidMount() {
if (window.location.hash) {
const id = window.location.hash.replace("#", "");
const element = document.getElementById(id);
element.scrollIntoView();
}
}
... You can see this working in codesandbox https://codesandbox.io/s/nifty-varahamihira-dj5qb |
Thank @RAFREX
|
@zcmgyu thanks! your custom Hook did help but i needed to did slight modification, any idea why? import { useEffect } from 'react'
export default function useScrollToHash() {
useEffect(() => {
//TODO: setTimeout with 0 made it work in Safari - i dont know why
setTimeout(() => {
const { hash } = window.location
if (hash) {
const id = hash.replace('#', '')
const element = document.getElementById(id)
if (element) {
element.scrollIntoView({ block: 'start', behavior: 'instant' })
window.scrollBy(0, -55)
}
}
}, 0)
}, [])
} |
In my case solution is wrap
|
There is another trick that you can try, but it only works in cases where you exactly know the size of your async result beforehand. For example lets say you have a website with an async search result of 100 Lines (+ maybe a pagination). Then you could prefill the 100 lines synchronously with dummy lines/anchors (empty string), just so that the native scroll anchor mechanism works. Then as soon as your promise gets resolved the real data is filled in and you are already in the correct scroll position. |
@cjke you may use this for reference. |
Yes this is exactly what I needed. Although I needed to remove the last Edit: I guess all credit actually goes to @zcmgyu |
How is this implemented into a class component? |
@rafgraph thanks but I think I was trying to force fit this issue to fit my problem, I will ask another issue to answer my question |
Note This may be out of scope for this package.
Thanks for this package, I have been following this issue for quite a while.
The link works perfectly, however if a reload / refresh occurs, the page no longer scrolls down into position.
The text was updated successfully, but these errors were encountered: