From 11abe9255a287db615e333e256fcf29453485e9e Mon Sep 17 00:00:00 2001 From: hamster1963 <1410514192@qq.com> Date: Tue, 22 Oct 2024 20:51:07 +0800 Subject: [PATCH] fix: hasHistory --- .../ClientComponents/ServerDetailClient.tsx | 5 ++--- .../ClientComponents/ServerListClient.tsx | 21 +++++++++++-------- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/app/[locale]/(main)/ClientComponents/ServerDetailClient.tsx b/app/[locale]/(main)/ClientComponents/ServerDetailClient.tsx index 74227c2..55a3e52 100644 --- a/app/[locale]/(main)/ClientComponents/ServerDetailClient.tsx +++ b/app/[locale]/(main)/ClientComponents/ServerDetailClient.tsx @@ -30,11 +30,10 @@ export default function ServerDetailClient({ useEffect(() => { const previousPath = sessionStorage.getItem("lastPath"); - const currentPath = window.location.pathname; - - if (previousPath && previousPath !== currentPath) { + if (previousPath) { setHasHistory(true); } else { + const currentPath = window.location.pathname; sessionStorage.setItem("lastPath", currentPath); } }, []); diff --git a/app/[locale]/(main)/ClientComponents/ServerListClient.tsx b/app/[locale]/(main)/ClientComponents/ServerListClient.tsx index 85425cc..707047f 100644 --- a/app/[locale]/(main)/ClientComponents/ServerListClient.tsx +++ b/app/[locale]/(main)/ClientComponents/ServerListClient.tsx @@ -12,11 +12,18 @@ import useSWR from "swr"; export default function ServerListClient() { const t = useTranslations("ServerListClient"); const containerRef = useRef(null); - const defaultTag = t("defaultTag"); - const [tag, setTag] = useState( - sessionStorage.getItem("selectedTag") || defaultTag, - ); + + const [tag, setTag] = useState(defaultTag); + + const [isMounted, setIsMounted] = useState(false); + useEffect(() => { + const savedTag = sessionStorage.getItem("selectedTag") || defaultTag; + setTag(savedTag); + + restoreScrollPosition(); + setIsMounted(true); + }, []); const handleTagChange = (newTag: string) => { setTag(newTag); @@ -34,10 +41,6 @@ export default function ServerListClient() { } }; - useEffect(() => { - restoreScrollPosition(); - }, [tag]); - useEffect(() => { const handleRouteChange = () => { restoreScrollPosition(); @@ -61,7 +64,7 @@ export default function ServerListClient() { ); - if (!data?.result) return null; + if (!data?.result || !isMounted) return null; const { result } = data; const sortedServers = result.sort((a, b) => {