Merge pull request #2117 from JunJD/优化useScrollToBottom

refactor: 优化 useScrollToBottom
This commit is contained in:
Yifei Zhang 2023-06-24 22:25:45 +08:00 committed by GitHub
commit fb82806956
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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();
});