feat: user依赖优化

This commit is contained in:
zdl
2025-10-24 12:34:43 +08:00
parent 13c3c74b92
commit e69f822150

View File

@@ -57,6 +57,11 @@ export default function TradingSimulation() {
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(() => {
const userIdChanged = prevUserIdRef.current !== userId;
const authChanged = prevIsAuthenticatedRef.current !== isAuthenticated;
if (userIdChanged || authChanged) {
prevUserIdRef.current = userId;
prevIsAuthenticatedRef.current = isAuthenticated;
logger.debug('TradingSimulation', '组件挂载,认证状态检查', {
isAuthenticated,
userId: user?.id,
userId,
userName: user?.name
});
}, [isAuthenticated, user?.id]); // 只依赖 user.id,避免无限循环
}
}, [isAuthenticated, userId, user]); // ⚡ 使用 userId防重复通过 ref 判断
// 获取资产历史数据的 useEffect
useEffect(() => {