-
Notifications
You must be signed in to change notification settings - Fork 47.5k
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: element.current.scrollIntoView() not working as expected in chrome. #23396
Comments
Note that Could you include a video of the behavior in Firefox and Chrome and explain with words what's "weird" about this behavior? Bonus: Could you convert the repro into a codesandbox.io? Makes it easier to debug the issue. |
I'm having the same problem.
|
This works like a charm. Thanks buddy |
Come accross this issue with
, that |
I had the same problem
|
@hwangyena cool little thing I found out -- you can do what you did It's basically just a trick to defer until next tick in JS. |
I had the same problem and managed to fix it by using the
Works fine in Firefox, Chrome, Safari, Edge scroll2.mp4 |
The problem doesn't seem to be limited to |
Not working in Electron 19 (Chromium) This was working in React 16, I've since updated the project to React 18 |
For me it works if I remove the |
@pjm4 Yeah, that was the case for me as well. This is what I did as a workaround: const [shouldScrollToRow, setShouldScrollToRow] = useState(false);
useEffect(() => {
if (shouldScrollToRow) {
setTimeout(() => {
elemRef.current?.scrollIntoView({ behavior: 'smooth', block: 'end' });
}, 100);
setShouldScrollToRow(false);
}
}, [shouldScrollToRow]);
useEffect(() => {
// Listening for items getting added
if (itemsUpdated) {
setShouldScrollToRow(true);
setItemsUpdated(false);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [itemsUpdated]); |
I want to know why this is happening |
A workaround I've found it to use
|
Just wanted to shed some light into this issue and why it can probably be closed. This has nothing to do with react. This is a problem that is related to the chromium implementation of the https://bugs.chromium.org/p/chromium/issues/detail?id=1121151 So from what I've got from all the testing and searching the last few days is that chromium based browsers interrupt the This only happens with the https://jsfiddle.net/2bnspw8e/8/ So then what's a solution if you want to keep the simplicity of There is a ponyfill version of this method which reimplements it using the non broken I hope this clarifies what's actually happening behind the scene and provides a solution while we all wait for a chromium fix. |
Thanks for the digging and, I always wanted a one-trick pony! This shows up at just the right time! Cheers! |
- switch from icon libraries to using custom icon SVGs- add's autofocus prop for ChatInput - fix scroll behavior in ChatPanel: (scrollIntoView issue with Chrome, but works in other browser like safari) facebook/react#23396 - switch from `yext/react-components`'s Markdown component to our own Markdown component since they will be removing that soon and `yext/react-components` also contains other packages/licenses that we don't want (e.g. mapbox) - Generate third party license file for the library J=CLIP-192 TEST=manual&auto see that scroll behavior works as expected in Chrome and Safari see that autofocus for ChatInput is off by default (rendering Panel way down in a page and see that it doesn't jump to it on load) see that markdown is handled properly see the icons are rendered as expected
I ended up creating my own helper function.
To call the function i use the following
|
IMO almost all the answers above are not correct. You are trying to invoke scrolling in Function inside effect executes in parallel to DOM update commits and in 99% it will be invoked faster. So normally real DOM (according to the vDOM built up with your render function) is not ready yet at that point of time. This is why "workarounds" using |
This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize it yet. If you have any new additional information, please include it with your comment! |
this is not stale, because it is not fixed |
This is not fixed for sure. |
I want to add that the
|
Hello. After checking different scenarios, I also concluded that there is an issue with the way Chrome behaves with In my case, the scroll randomly stops at the start or middle of the scroll.
|
In my case I`ve encountered weird behavior with simple onPress event with window.scrollTo({ top: scrollRef.current.offsetTop, behavior: 'smooth' }); So I've wrapped this call with setTimeout 0ms and it fixed unexpected behavior (stops mid scroll) completely. |
This was the issue in my case. Thanks mate! |
Any updates on this? I still seem to be getting this exact issue on Chrome. Removing |
You should probably look into related chrome issue instead. This is a chrome bug that has nothing to do with react. https://issues.chromium.org/issues/325081538 The recent comment says they are changing how smooth in to view smooth work in chrome, which may fix this issue. |
Still not working for me in either Chrome or Firefox on desktop. I enclose an excerpt from some messaging functionality - users can open and close a chat window from a list of available conversations. The first time I open the window, the content appears but the window doesn't scroll. If I then close and reopen the window it works as intended, as does posting new text to the window. Note: The placement of scrollIntoView() is in two different places within these methods - this is intentionally done to test the behaviour; afaik both should work, neither does.
Output in console, whether on first or subsequent opens, is as follows :
|
When you reload the page, once the page scrolled to the <h1> that has `ref`, but it scrolls back to the top of the page. It may the problem on the Chrome. facebook/react#23396
I also faced this same issue, had to create my own custom function, |
In my use case, the following code snippet worked very well: divRef.current?.scroll({
top: divRef.current?.scrollHeight,
behavior: 'smooth'
}); |
useEffect(() => {
if (itemsUpdated) {
setTimeout(() => {
window.scrollTo({
top: ref.current.offsetTop,
});
}, 100);
}
}, [
itemsUpdated // when you needed
]); |
Hey πββοΈ
I have built a simple chatbot using react, and when the user sends a message i want to show the last message in the chatWindow component, so I used the following code:
It works as expected in edge and Firefox, but on chrome it is behaving weird
llink-to-the-chatbot: https://karthik2265.github.io/worlds-best-chatbot/
github-repo-link: https://github.com/karthik2265/worlds-best-chatbot
Thank you
The text was updated successfully, but these errors were encountered: