From 4e7c714ea2c65366f9fc3355a9d3a35d678f07d9 Mon Sep 17 00:00:00 2001 From: icarus Date: Tue, 14 Oct 2025 18:04:33 +0800 Subject: [PATCH] fix(MinAppPage): replace useRef with useState for initial navbar state fix: Error: Cannot access refs during render --- src/renderer/src/pages/minapps/MinAppPage.tsx | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/renderer/src/pages/minapps/MinAppPage.tsx b/src/renderer/src/pages/minapps/MinAppPage.tsx index 3da51c0c6b..ad70ad0da4 100644 --- a/src/renderer/src/pages/minapps/MinAppPage.tsx +++ b/src/renderer/src/pages/minapps/MinAppPage.tsx @@ -28,7 +28,9 @@ const MinAppPage: FC = () => { const navigate = useNavigate() // Remember the initial navbar position when component mounts - const initialIsTopNavbar = useRef(isTopNavbar) + // It's immutable state + const [initialIsTopNavbar] = useState(isTopNavbar) + const initialIsTopNavbarRef = useRef(initialIsTopNavbar) const hasRedirected = useRef(false) // Initialize TabsService with cache reference @@ -40,8 +42,8 @@ const MinAppPage: FC = () => { // Debug: track navbar position changes useEffect(() => { - if (initialIsTopNavbar.current !== isTopNavbar) { - logger.debug(`NavBar position changed from ${initialIsTopNavbar.current} to ${isTopNavbar}`) + if (initialIsTopNavbarRef.current !== isTopNavbar) { + logger.debug(`NavBar position changed from ${initialIsTopNavbarRef.current} to ${isTopNavbar}`) } }, [isTopNavbar]) @@ -69,7 +71,7 @@ const MinAppPage: FC = () => { // For sidebar navigation, redirect to apps list and open popup // Only check once and only if we haven't already redirected - if (!initialIsTopNavbar.current && !hasRedirected.current) { + if (!initialIsTopNavbarRef.current && !hasRedirected.current) { hasRedirected.current = true navigate('/apps') // Open popup after navigation @@ -80,11 +82,11 @@ const MinAppPage: FC = () => { } // For top navbar mode, integrate with cache system - if (initialIsTopNavbar.current) { + if (initialIsTopNavbarRef.current) { // 无论是否已在缓存,都调用以确保 currentMinappId 同步到路由切换的新 appId openMinappKeepAlive(app) } - }, [app, navigate, openMinappKeepAlive, initialIsTopNavbar]) + }, [app, navigate, openMinappKeepAlive]) // -------------- 新的 Tab Shell 逻辑 -------------- // 注意:Hooks 必须在任何 return 之前调用,因此提前定义,并在内部判空 @@ -160,7 +162,7 @@ const MinAppPage: FC = () => { }, [app, isReady]) // 如果条件不满足,提前返回(所有 hooks 已调用) - if (!app || !initialIsTopNavbar.current) { + if (!app || !initialIsTopNavbar) { return null }