Browse Source

Merge pull request #2117 from JunJD/优化useScrollToBottom

refactor: 优化 useScrollToBottom
Yifei Zhang 1 year ago
parent
commit
fb82806956
1 changed files with 5 additions and 5 deletions
  1. 5 5
      app/components/chat.tsx

+ 5 - 5
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<HTMLDivElement>(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();
   });