From f0ccf0cea7a20817f0af28e059fc7a50472d7e6b Mon Sep 17 00:00:00 2001 From: Anh Thu Nguyen Date: Fri, 3 Jan 2025 11:40:07 -0500 Subject: [PATCH] remove event listener on return --- src/components/ChatPanel.tsx | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/components/ChatPanel.tsx b/src/components/ChatPanel.tsx index 6888c2e..d000a2f 100644 --- a/src/components/ChatPanel.tsx +++ b/src/components/ChatPanel.tsx @@ -211,14 +211,18 @@ export function ChatPanel(props: ChatPanelProps) { useLayoutEffect(() => { const curr = messagesContainer.current; - curr?.addEventListener("scroll", () => { + const onScroll = () => { if (!conversationId) { return; } saveSessionState(conversationId, { - scrollPosition: curr.scrollTop, + scrollPosition: curr?.scrollTop, }); - }); + } + curr?.addEventListener("scroll", onScroll); + return () => { + curr?.removeEventListener("scroll", onScroll); + }; }, [messagesContainer, conversationId]); return (