Closed ysong10 closed 2 years ago
I have solved this problem. In case other people met this problem, I will describe how I solved this problem in details. Basically, the problem is that every time there is message bubble coming or I input something, the two states (messages and input ) are changed, then useEffect is called
useEffect(() => {
scrollIntoView();
});
so chatContainerRef.current.scrollTop
will be set to the bottom in the scrollIntoView()
every time.
Solution:
const scrollIntoViewNotToBottom = () => {
setTimeout(() => {
setInputValue("");
// get div element by chatContainerRef.current
chatContainerRef.current.scrollTop = chatContainerRef.current.scrollHeight - 30;
}, 50);
};
and call this method in the method handleValidMessage
const handleValidMessage = (userMsg) => {
setState((state) => ({
...state,
messages: [...state.messages, userMsg],
}));
scrollIntoViewNotToBottom();
};
useEffect(() =>{
scrollIntoViewNotToBottom();
}, [messages])
useEffect(() => { scrollIntoView(); },[input]);
Some answers from our chatbot are very long. The chatbot scrolls automatically to the bottom so users have to scroll up to get to the top of the bubble and start reading. So can you tell me how to change the code to solve this, please?