Skip to content

Commit

Permalink
Merge pull request ChatGPTNextWeb#2117 from JunJD/优化useScrollToBottom
Browse files Browse the repository at this point in the history
refactor: 优化 useScrollToBottom
  • Loading branch information
Yidadaa authored Jun 24, 2023
2 parents 0658a93 + 9278d7f commit fb82806
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions app/components/chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import React, {
useState,
useRef,
useEffect,
useLayoutEffect,
useMemo,
useCallback,
} from "react";

import SendWhiteIcon from "../icons/send-white.svg";
Expand Down Expand Up @@ -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();
});

Expand Down

0 comments on commit fb82806

Please sign in to comment.