fix(CompanyOverview): 修复 React Strict Mode 下骨架屏闪现问题
- 移除所有 hooks 中的 finally 块,避免请求取消时错误更新状态 - 添加 hasLoaded 状态追踪首次加载完成 - CanceledError 时直接返回,不更新任何状态 - 使用派生 isLoading 状态确保骨架屏正确显示 修复的 hooks: - useShareholderData.ts - useManagementData.ts - useAnnouncementsData.ts - useDisclosureData.ts - useBasicInfo.ts 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -34,12 +34,16 @@ export const useDisclosureData = (options: UseDisclosureDataOptions): UseDisclos
|
||||
const { stockCode, enabled = true } = options;
|
||||
|
||||
const [disclosureSchedule, setDisclosureSchedule] = useState<DisclosureSchedule[]>([]);
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [hasLoaded, setHasLoaded] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
// 只有 enabled 且有 stockCode 时才请求
|
||||
if (!enabled || !stockCode) return;
|
||||
if (!enabled || !stockCode) {
|
||||
setLoading(false);
|
||||
return;
|
||||
}
|
||||
|
||||
const controller = new AbortController();
|
||||
|
||||
@@ -58,12 +62,17 @@ export const useDisclosureData = (options: UseDisclosureDataOptions): UseDisclos
|
||||
} else {
|
||||
setError("加载披露日程数据失败");
|
||||
}
|
||||
setLoading(false);
|
||||
setHasLoaded(true);
|
||||
} catch (err: any) {
|
||||
if (err.name === "CanceledError") return;
|
||||
// 请求被取消时,不更新任何状态
|
||||
if (err.name === "CanceledError") {
|
||||
return;
|
||||
}
|
||||
logger.error("useDisclosureData", "loadData", err, { stockCode });
|
||||
setError("网络请求失败");
|
||||
} finally {
|
||||
setLoading(false);
|
||||
setHasLoaded(true);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -71,5 +80,7 @@ export const useDisclosureData = (options: UseDisclosureDataOptions): UseDisclos
|
||||
return () => controller.abort();
|
||||
}, [stockCode, enabled]);
|
||||
|
||||
return { disclosureSchedule, loading, error };
|
||||
const isLoading = loading || (enabled && !hasLoaded && !error);
|
||||
|
||||
return { disclosureSchedule, loading: isLoading, error };
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user