HELP, Best way to limit chat by removing oldest message without breaking autoscroll #1244
Unanswered
encoderpie
asked this question in
Q&A
Replies: 1 comment
|
i have created a codesandbox to help you better understand the problem and have included the link below: https://codesandbox.io/p/sandbox/react-virtuoso-v1-chat-forked-n728w6 When you open the CodeSandbox and preview it, you can press the "send 50" or "send 500" buttons to observe the autoscroll issues more clearly. Please help me resolve this error. |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment

Uh oh!
There was an error while loading. Please reload this page.
Describe the bug
I’m building a chat application using
react-virtuosowhere I need to limit the number of messages displayed (e.g.,MAX_MESSAGES = 500). When a new message is added, I remove the oldest message to keep the list within the limit. However, this approach causes issues with Virtuoso’s autoscroll behavior:followOutputset to'auto'), removing the oldest message causes the autoscroll to stop unexpectedly.The root cause seems to be that removing messages from the top of the list affects Virtuoso’s virtual scrolling calculations, leading to inconsistent scroll behavior.
To Reproduce
Steps to reproduce the behavior:
followOutputis set to'auto').MAX_MESSAGES(e.g., 500 messages).Expected behavior
I expect Virtuoso to maintain smooth autoscroll behavior even when the oldest message is removed from the list to enforce a message limit. When a new message is added, Virtuoso should reliably scroll to the bottom without stopping or leaving a gap.
Screenshots
Here’s a screenshot showing the issue:
Desktop (please complete the following information):
Additional context
I’m trying to implement a chat application where I need to keep the message list within a certain limit (e.g., 500 messages) for performance reasons. My current approach is to use
sliceto remove the oldest message when a new one is added:However, this causes the autoscroll issues described above. I’ve tried delaying the scroll with
setTimeoutand usingscrollToIndex, but the problem persists. I’d like to learn the best way to limit the chat by removing the oldest message without breaking Virtuoso’s autoscroll behavior. Any guidance or recommended approaches would be greatly appreciated!All reactions