From b946b44df30d0ea0c931147f2f133dae2a55c436 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20B=C3=A4cker?= Date: Thu, 9 Apr 2026 20:07:19 +0200 Subject: [PATCH] fix: derive non-marker children synchronously to avoid stale map layers --- lib/ClusteredMapView.js | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/lib/ClusteredMapView.js b/lib/ClusteredMapView.js index 8c7820c0..b3976621 100644 --- a/lib/ClusteredMapView.js +++ b/lib/ClusteredMapView.js @@ -51,7 +51,6 @@ const ClusteredMapView = forwardRef( ) => { const [markers, updateMarkers] = useState([]); const [spiderMarkers, updateSpiderMarker] = useState([]); - const [otherChildren, updateChildren] = useState([]); const [superCluster, setSuperCluster] = useState(null); const [currentRegion, updateRegion] = useState( restProps.region || restProps.initialRegion @@ -66,14 +65,21 @@ const ClusteredMapView = forwardRef( [children] ); + // Derive synchronously — storing non-markers (polylines, overlays) in state + useEffect + // leaves stale native views when those children are removed (e.g. toggling map layers). + const nonMarkerChildren = useMemo(() => { + if (!clusteringEnabled) { + return propsChildren; + } + return propsChildren.filter((child) => !isMarker(child)); + }, [propsChildren, clusteringEnabled]); + useEffect(() => { const rawData = []; - const otherChildren = []; if (!clusteringEnabled) { updateSpiderMarker([]); updateMarkers([]); - updateChildren(propsChildren); setSuperCluster(null); return; } @@ -81,8 +87,6 @@ const ClusteredMapView = forwardRef( propsChildren.forEach((child, index) => { if (isMarker(child)) { rawData.push(markerToGeoJSONFeature(child, index)); - } else { - otherChildren.push(child); } }); @@ -102,7 +106,6 @@ const ClusteredMapView = forwardRef( const markers = superCluster.getClusters(bBox, zoom); updateMarkers(markers); - updateChildren(otherChildren); setSuperCluster(superCluster); superClusterRef.current = superCluster; @@ -212,7 +215,7 @@ const ClusteredMapView = forwardRef( ) ) : null )} - {otherChildren} + {nonMarkerChildren} {spiderMarkers.map((marker) => { return propsChildren[marker.index] ? React.cloneElement(propsChildren[marker.index], {