From 7bb47a5b6bb46d919eeca1182edde6f7a2ff6028 Mon Sep 17 00:00:00 2001 From: Shinji-Li <1349021570@qq.com> Date: Wed, 17 Jan 2024 15:26:15 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20fix:=20=E5=85=BC=E5=AE=B9=20NextJs?= =?UTF-8?q?=20=E7=9A=84=20window=20=E4=B8=8D=E5=AD=98=E5=9C=A8=E9=97=AE?= =?UTF-8?q?=E9=A2=98=20(#38)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/ProChat/components/ScrollAnchor/index.tsx | 24 +++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/ProChat/components/ScrollAnchor/index.tsx b/src/ProChat/components/ScrollAnchor/index.tsx index b744a6ed..299a872c 100644 --- a/src/ProChat/components/ScrollAnchor/index.tsx +++ b/src/ProChat/components/ScrollAnchor/index.tsx @@ -1,4 +1,4 @@ -import { memo, useEffect } from 'react'; +import { memo, useEffect, useState } from 'react'; import { useInView } from 'react-intersection-observer'; import { useStore } from '@/ProChat/store'; @@ -10,15 +10,31 @@ const ChatScrollAnchor = memo(() => { const trackVisibility = useStore((s) => !!s.chatLoadingId); const str = useStore(chatSelectors.currentChats); + const [isWindowAvailable, setIsWindowAvailable] = useState(false); + + useEffect(() => { + // 检查window对象是否已经可用 + if (typeof window !== 'undefined') { + setIsWindowAvailable(true); + } + }, []); + const { ref, entry, inView } = useInView({ delay: 100, rootMargin: '0px 0px -150px 0px', trackVisibility, }); - // 如果是移动端,可能200太多了,认为超过 1/3 即可,PC默认200 - const ScrollOffset = window.innerHeight / 3 > 200 ? 200 : window.innerHeight / 4; - const isAtBottom = useAtBottom(ScrollOffset); + const [scrollOffset, setScrollOffset] = useState(0); + + useEffect(() => { + if (isWindowAvailable) { + // 如果是移动端,可能200太多了,认为超过 1/3 即可,PC默认200 + setScrollOffset(window.innerHeight / 3 > 200 ? 200 : window.innerHeight / 4); + } + }, [isWindowAvailable]); + + const isAtBottom = useAtBottom(scrollOffset); useEffect(() => { if (isAtBottom && trackVisibility && !inView) {