react: Bug: element.current.scrollIntoView() not working as expected in chrome.
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:
useEffect(
function () {
if (lastmessageRef.current !== null) {
lastmessageRef.current.scrollIntoView()
}
},
[lastmessageRef]
)
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
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 20
- Comments: 36
Commits related to this issue
- Custom icon SVG and QA feedback (#12) - switch from icon libraries to using custom icon SVGs- add's autofocus prop for ChatInput - fix scroll behavior in ChatPanel: (scrollIntoView issue with Chrome... — committed to yext/chat-ui-react by yen-tt 3 years ago
- Fix scroll issue using scrollTo() instead of scrollIntoView() 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 prob... — committed to noriyotcp/react-training-2024 by noriyotcp 2 years ago
- Add workaround for Chrome smooth scroll bug by doing it twice Ref: https://github.com/facebook/react/issues/23396 — committed to ente-io/ente by mnvr 2 years ago
- Add workaround for Chrome smooth scroll bug by doing it twice Ref: https://github.com/facebook/react/issues/23396 — committed to ente-io/ente by mnvr 2 years ago
A workaround I’ve found it to use
window.requestAnimationFrame: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
scrollIntoViewmethod. There’s been some open issues for a long time in the chromium project regarding this issue.https://bugs.chromium.org/p/chromium/issues/detail?id=1121151 https://bugs.chromium.org/p/chromium/issues/detail?id=1043933 https://bugs.chromium.org/p/chromium/issues/detail?id=833617
So from what I’ve got from all the testing and searching the last few days is that chromium based browsers interrupt the
scrollIntoViewwhen any other scroll event is triggered.This only happens with the
scrollIntoViewmethod, thescrollToandscrollmethods don’t suffer from this issue. You can see and test this by yourself on the following fiddle.https://jsfiddle.net/2bnspw8e/8/
So then what’s a solution if you want to keep the simplicity of
scrollIntoViewand have other scroll happen at the same time?There is a ponyfill version of this method which reimplements it using the non broken
scrollmethod.I hope this clarifies what’s actually happening behind the scene and provides a solution while we all wait for a chromium fix.
For me it works if I remove the
behavior: 'smooth',prop, however, I really want the smooth scrolling behaviour 😦I had the same problem So I solved this by using setTimeout. (The flicker looks weird, but it works.)
IMO almost all the answers above are not correct.
You are trying to invoke scrolling in
Effectbut it should be done inLayoutEffect.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
setTimeoutwork somehow. But usingLayoutEffectis more accurate.I’m having the same problem. scrollIntoView api doesn’t work inside useEffect, while it fires by onClick. Worth mentioning that it works in Firefox. Here is the code:
I had the same problem and managed to fix it by using the
scrollmethod for the parent node instead ofscrollIntoViewfor the children node:Works fine in Firefox, Chrome, Safari, Edge
https://user-images.githubusercontent.com/26645888/217844587-0b5d64d7-f34d-4128-8e43-9b7cefdc9403.mp4
@hwangyena cool little thing I found out – you can do what you did
setTimeout(..., 0)and it works as well without delay, at least in my use case.It’s basically just a trick to defer until next tick in JS.
I want to know why this is happening
Hello.
After checking different scenarios, I also concluded that there is an issue with the way Chrome behaves with
scrollIntoView()when you set the behavior tosmooth.In my case, the scroll randomly stops at the start or middle of the scroll. A temporary solution for me would be to disable the smooth scroll for Chrome browsers.
@pjm4 Yeah, that was the case for me as well.
This is what I did as a workaround:
Note that
lastmessageRefas a dependency won’t do anything and can be removed. The effect won’t fire iflastmessageRef.currentchanges.Could you include a video of the behavior in Firefox and Chrome and explain with words what’s “weird” about this behavior? Also please include the exact version of those browsers and the operating system you’re using.
Bonus: Could you convert the repro into a codesandbox.io? Makes it easier to debug the issue.
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.
Any updates on this?
I still seem to be getting this exact issue on Chrome. Removing
behaviour: smoothworks.This is not fixed for sure.
behavior: 'smooth'does work on FF but not on Chrome. I have codes to reproduce if you wish.this is not stale, because it is not fixed
Thanks for the digging and, I always wanted a one-trick pony!
This shows up at just the right time!
Cheers!
Come accross this issue with
, that
element.current.scrollIntoViewworks in FF but not in Chrome, is there any progress?This works like a charm. Thanks buddy