From f4cf5d1cb659bc7e41dfae25c0dd38f4accc3029 Mon Sep 17 00:00:00 2001 From: kkfluous Date: Sat, 28 Mar 2026 23:30:07 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20useMemo=E7=A7=BB=E5=88=B0early=20return?= =?UTF-8?q?=E4=B9=8B=E5=89=8D=EF=BC=8C=E4=BF=AE=E5=A4=8Dhooks=E9=A1=BA?= =?UTF-8?q?=E5=BA=8F=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit React要求hooks在每次渲染中顺序一致,不能在条件return之后调用。 Co-Authored-By: Claude Opus 4.6 (1M context) --- src/App.tsx | 48 ++++++++++++++++++++++++------------------------ 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index 5fbfe86..f965061 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -372,6 +372,30 @@ export default function App() { return mp; }), [modalWeeklyDetail, modalFilters.plateNumber]); + const customerPieData = useMemo(() => { + let data: { name: string; value: number }[] = []; + if (customerChartView === 'region') { + const map: Record = {}; + customerData.forEach(item => { map[item.region] = (map[item.region] || 0) + item.total; }); + data = Object.entries(map).map(([name, value]) => ({ name, value })).sort((a, b) => b.value - a.value); + } else { + const map: Record = {}; + customerData.forEach(item => { map[item.city] = (map[item.city] || 0) + item.total; }); + const tot = Object.values(map).reduce((a, b) => a + b, 0); + const threshold = tot * 0.05; + let other = 0; + Object.entries(map).forEach(([name, value]) => { + if (value >= threshold) data.push({ name, value }); + else other += value; + }); + if (other > 0) data.push({ name: '其他', value: other }); + data.sort((a, b) => b.value - a.value); + } + return data; + }, [customerData, customerChartView]); + + const watermarkText = useMemo(() => `羚牛氢能-${new Date().toLocaleString('zh-CN', { year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', second: '2-digit', hour12: false }).replace(/\//g, '-')}`, [lastUpdate]); + if (loading && !summary) { return (
@@ -399,30 +423,6 @@ export default function App() { const SUMMARY = summary!; - const customerPieData = useMemo(() => { - let data: { name: string; value: number }[] = []; - if (customerChartView === 'region') { - const map: Record = {}; - customerData.forEach(item => { map[item.region] = (map[item.region] || 0) + item.total; }); - data = Object.entries(map).map(([name, value]) => ({ name, value })).sort((a, b) => b.value - a.value); - } else { - const map: Record = {}; - customerData.forEach(item => { map[item.city] = (map[item.city] || 0) + item.total; }); - const tot = Object.values(map).reduce((a, b) => a + b, 0); - const threshold = tot * 0.05; - let other = 0; - Object.entries(map).forEach(([name, value]) => { - if (value >= threshold) data.push({ name, value }); - else other += value; - }); - if (other > 0) data.push({ name: '其他', value: other }); - data.sort((a, b) => b.value - a.value); - } - return data; - }, [customerData, customerChartView]); - - const watermarkText = useMemo(() => `羚牛氢能-${new Date().toLocaleString('zh-CN', { year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', second: '2-digit', hour12: false }).replace(/\//g, '-')}`, [lastUpdate]); - return (
{/* Watermark */}