forked from XiaoMo/ChatGPT-Next-Web
refactor: 优化 useScrollToBottom
This commit is contained in:
parent
35ba898c97
commit
9278d7f851
@ -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();
|
||||
});
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user