From 477a9d5c02b2f1ede1ebfa8c0c4f396562303262 Mon Sep 17 00:00:00 2001 From: mahula Date: Thu, 20 Nov 2025 20:10:31 +0100 Subject: [PATCH] fix: prevent duplicate API instances in MapContainer - fixed state management bug where API instances were appended to existing array instead of replacing it - this caused duplicate API instances in React Strict Mode and potential memory leaks - replaced setApis((current) => [...current, newApi]) with proper array replacement - compute full API array from layers, then replace state completely - use .map() for transformation instead of side effects - the apis array has 1:1 relationship with layers (matched by ID), so accumulation was never intended --- app/src/pages/MapContainer.tsx | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/app/src/pages/MapContainer.tsx b/app/src/pages/MapContainer.tsx index 4bf983a4..44f73bea 100644 --- a/app/src/pages/MapContainer.tsx +++ b/app/src/pages/MapContainer.tsx @@ -36,12 +36,12 @@ function MapContainer({ layers, map }: { layers: LayerProps[]; map: any }) { const [apis, setApis] = useState([]) useEffect(() => { - // get timestamp for the end of the current day + // get timestamp for the start of the current day const now = new Date() const startOfDay = new Date(now.getFullYear(), now.getMonth(), now.getDate()) const startOfDayISO = startOfDay.toISOString() - layers.map((layer: LayerProps) => { + const newApis = layers.map((layer: LayerProps) => { // Only apply date filter if showPastItems is not explicitly set to true const dateFilter = layer.showPastItems ? undefined @@ -60,15 +60,13 @@ function MapContainer({ layers, map }: { layers: LayerProps[]; map: any }) { ], } - apis && - setApis((current) => [ - ...current, - { - id: layer.id, - api: new itemsApi('items', layer.id, undefined, dateFilter), - }, - ]) + return { + id: layer.id, + api: new itemsApi('items', layer.id, undefined, dateFilter), + } }) + + setApis(newApis) }, [layers]) useEffect(() => {}, [apis])