diff --git a/src/views/Dashboard/Center.js b/src/views/Dashboard/Center.js index dcc01902..4a04ef5f 100644 --- a/src/views/Dashboard/Center.js +++ b/src/views/Dashboard/Center.js @@ -74,9 +74,8 @@ export default function CenterDashboard() { const navigate = useNavigate(); const toast = useToast(); - // ⚡ 提取 userId 为独立变量,避免 user 对象引用变化导致无限循环 + // ⚡ 提取 userId 为独立变量 const userId = user?.id; - const prevUserIdRef = React.useRef(userId); // 🎯 初始化Dashboard埋点Hook const dashboardEvents = useDashboardEvents({ @@ -241,32 +240,16 @@ export default function CenterDashboard() { return 'green'; }; + // 🔧 使用 ref 跟踪是否已经加载过数据(首次加载标记) + const hasLoadedRef = React.useRef(false); + useEffect(() => { - console.log('[Center] 🔥 useEffect 触发', { - userId, - prevUserId: prevUserIdRef.current, - pathname: location.pathname, - hasUser: !!user, - time: new Date().toISOString() - }); + const isOnCenterPage = location.pathname.includes('/home/center'); - const userIdChanged = prevUserIdRef.current !== userId; - - if (userIdChanged) { - console.log('[Center] ⚠️ userId 发生变化:', prevUserIdRef.current, '->', userId); - prevUserIdRef.current = userId; - } - - // 只在 userId 真正变化或路径变化时加载数据 - const shouldLoad = (userIdChanged || !prevUserIdRef.current) && user && location.pathname.includes('/home/center'); - console.log('[Center] 🤔 是否应该加载数据:', shouldLoad, { - userIdChanged, - prevUserIdRef: prevUserIdRef.current, - hasUser: !!user, - pathMatch: location.pathname.includes('/home/center') - }); - - if (shouldLoad) { + // 首次进入页面且有用户时加载数据 + if (user && isOnCenterPage && !hasLoadedRef.current) { + console.log('[Center] 🚀 首次加载数据'); + hasLoadedRef.current = true; loadData(); } @@ -278,7 +261,14 @@ export default function CenterDashboard() { }; document.addEventListener('visibilitychange', onVis); return () => document.removeEventListener('visibilitychange', onVis); - }, [userId, location.pathname, loadData, user]); // ⚡ 使用 userId,防重复通过 ref 判断 + }, [userId, location.pathname, loadData, user]); + + // 当用户登出再登入(userId 变化)时,重置加载标记 + useEffect(() => { + if (!user) { + hasLoadedRef.current = false; + } + }, [user]); // 定时刷新实时行情(每分钟一次) useEffect(() => {