I have list of comments. Every time user adds a new comment, I want that div and that div only to scroll to bottom. That div height is fixed and overflow is also set to scroll. I have found some answers to use dummy-div at the end of the comment list to scroll. I encountered 2 problems with that approach.
When used scrollIntoView to that div element, the entire body scrolls. Which is unwanted. I just want that div body to scroll to the last comment.
Say, I have a function, upon calling, scrolls to bottom.
handleScrollToBottom. I called it right after the comment was added. But, it doesn't scroll to the bottom. Which makes sense. Because, if I am not wrong, updating the state doesn't update the state instantly. Let me demonstrate more with code
const Comment = () => {
const ref = useRef();
const handleScrollToBottom = () => {
//
ref.current.scrollIntoView();
};
const addComment = () => {
//
setAllComments((pre) => [...pre, { cmt: newCmt }]);
// Doesn't work here
handleScrollToBottom();
};
return (
<div>
{allComments.map((cmt) => (
<Comment cmt={cmt} />
))}
{/* Dummy Div to scroll to bottom */}
<div ref={ref}></div>
</div>
);
};
That was the second problem. So, I used useEffect and put allComments.length as a dependency.
useEffect(() => {
// Deleting a comment calls this function
handleScrollToBottom();
}, [allComments.length]);
That does work. But, the problem is, when a comment was deleted, it scrolls to bottom becasue the comments length changes which is not desired. Does anyone have any solution for my problem? I really appreciate it.