From c9d9e966651efb3b142815f0f1d246853a33aa8a Mon Sep 17 00:00:00 2001 From: Condor Hero Date: Wed, 25 Dec 2024 22:26:18 +0800 Subject: [PATCH] fix: white page when toggling between open and close in tabbar mode(/about?a=10) --- src/layout/layout-tabbar/index.tsx | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/src/layout/layout-tabbar/index.tsx b/src/layout/layout-tabbar/index.tsx index f5c4887..0fad04b 100644 --- a/src/layout/layout-tabbar/index.tsx +++ b/src/layout/layout-tabbar/index.tsx @@ -166,20 +166,22 @@ export default function LayoutTabbar() { useEffect(() => { const activePath = location.pathname; const normalizedPath = removeTrailingSlash(activePath); + // tabbarEnable 变量会导致本组件挂载和卸载,normalizedPath 可能与 activeKey 相同,增加判断防止 addTab 重复添加 + if (normalizedPath !== activeKey) { + setActiveKey(normalizedPath); - setActiveKey(normalizedPath); + const routeTitle = currentRoute.handle?.title; - const routeTitle = currentRoute.handle?.title; - - addTab(normalizedPath, { - key: normalizedPath, - // 保证 label 为 string 类型,存储到 sessionStorage。 - label: isValidElement(routeTitle) ? routeTitle?.props?.children : routeTitle, - historyState: { search: location.search, hash: location.hash }, - /* 登录之后跳转的默认路由,不可以关闭和拖拽 */ - closable: normalizedPath !== import.meta.env.VITE_BASE_HOME_PATH, - draggable: normalizedPath !== import.meta.env.VITE_BASE_HOME_PATH, - }); + addTab(normalizedPath, { + key: normalizedPath, + // 保证 label 为 string 类型,存储到 sessionStorage。 + label: isValidElement(routeTitle) ? routeTitle?.props?.children : routeTitle, + historyState: { search: location.search, hash: location.hash }, + /* 登录之后跳转的默认路由,不可以关闭和拖拽 */ + closable: normalizedPath !== import.meta.env.VITE_BASE_HOME_PATH, + draggable: normalizedPath !== import.meta.env.VITE_BASE_HOME_PATH, + }); + } }, [location, currentRoute, setActiveKey, addTab]); return (