From e69f8221507ddf5e6030adb09bc22ec7a37afc31 Mon Sep 17 00:00:00 2001 From: zdl <3489966805@qq.com> Date: Fri, 24 Oct 2025 12:34:43 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20user=E4=BE=9D=E8=B5=96=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/views/TradingSimulation/index.js | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/src/views/TradingSimulation/index.js b/src/views/TradingSimulation/index.js index e8d145c0..a1d8d03d 100644 --- a/src/views/TradingSimulation/index.js +++ b/src/views/TradingSimulation/index.js @@ -56,7 +56,12 @@ export default function TradingSimulation() { const { user, isAuthenticated } = useAuth(); const [activeTab, setActiveTab] = useState(0); const [assetHistory, setAssetHistory] = useState([]); // 移到这里! - + + // ⚡ 提取 userId 为独立变量,避免 user 对象引用变化导致无限循环 + const userId = user?.id; + const prevUserIdRef = React.useRef(userId); + const prevIsAuthenticatedRef = React.useRef(isAuthenticated); + // 使用模拟账户管理 Hook const { account, @@ -87,12 +92,20 @@ export default function TradingSimulation() { // 调试:观察认证状态变化 useEffect(() => { - logger.debug('TradingSimulation', '组件挂载,认证状态检查', { - isAuthenticated, - userId: user?.id, - userName: user?.name - }); - }, [isAuthenticated, user?.id]); // 只依赖 user.id,避免无限循环 + const userIdChanged = prevUserIdRef.current !== userId; + const authChanged = prevIsAuthenticatedRef.current !== isAuthenticated; + + if (userIdChanged || authChanged) { + prevUserIdRef.current = userId; + prevIsAuthenticatedRef.current = isAuthenticated; + + logger.debug('TradingSimulation', '组件挂载,认证状态检查', { + isAuthenticated, + userId, + userName: user?.name + }); + } + }, [isAuthenticated, userId, user]); // ⚡ 使用 userId,防重复通过 ref 判断 // 获取资产历史数据的 useEffect useEffect(() => {