From 9278d7f851a86d0681fd245a816e251638d5d6bd Mon Sep 17 00:00:00 2001 From: dingjunjie Date: Sat, 24 Jun 2023 16:37:29 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BC=98=E5=8C=96=20useScrollToBot?= =?UTF-8?q?tom?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/components/chat.tsx | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/app/components/chat.tsx b/app/components/chat.tsx index 047607d4e67..35f2804668b 100644 --- a/app/components/chat.tsx +++ b/app/components/chat.tsx @@ -3,8 +3,8 @@ import React, { useState, useRef, useEffect, - useLayoutEffect, useMemo, + useCallback, } from "react"; import SendWhiteIcon from "../icons/send-white.svg"; @@ -341,15 +341,15 @@ function useScrollToBottom() { // for auto-scroll const scrollRef = useRef(null); const [autoScroll, setAutoScroll] = useState(true); - const scrollToBottom = () => { + const scrollToBottom = useCallback(() => { const dom = scrollRef.current; if (dom) { - setTimeout(() => (dom.scrollTop = dom.scrollHeight), 1); + requestAnimationFrame(() => dom.scrollTo(0, dom.scrollHeight)); } - }; + }, []); // auto scroll - useLayoutEffect(() => { + useEffect(() => { autoScroll && scrollToBottom(); });