From 6ef5734b2b46fcd0ec4dcfda58480d1c48fd1bc6 Mon Sep 17 00:00:00 2001 From: Ryan Rondeau Date: Mon, 6 Jan 2025 14:20:55 -0800 Subject: [PATCH] fix: Clear Event Listeners On Unmount (#851) --- .../mapping/leaflet-map-with-server-side-clustering.tsx | 4 ++-- frontend/src/app/components/mapping/spiderfy.tsx | 8 ++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/frontend/src/app/components/mapping/leaflet-map-with-server-side-clustering.tsx b/frontend/src/app/components/mapping/leaflet-map-with-server-side-clustering.tsx index 679b83bcc..583f6a803 100644 --- a/frontend/src/app/components/mapping/leaflet-map-with-server-side-clustering.tsx +++ b/frontend/src/app/components/mapping/leaflet-map-with-server-side-clustering.tsx @@ -69,8 +69,8 @@ const LeafletMapWithServerSideClustering: React.FC = ({ ), ); - // Fit the map to the bounds - mapRef?.current?.fitBounds(bounds, { padding: [35, 35] }); + // Fit the map to the bounds. Disable animation due to known Leaflet issue if map unmounts while animating: https://github.com/Leaflet/Leaflet/issues/9527 + mapRef?.current?.flyToBounds(bounds, { padding: [35, 35], animate: false }); } else if (defaultClusterView.center && defaultClusterView.zoom) { mapRef?.current?.setView(defaultClusterView.center, defaultClusterView.zoom); } diff --git a/frontend/src/app/components/mapping/spiderfy.tsx b/frontend/src/app/components/mapping/spiderfy.tsx index 6dc8c8482..fd84fbb1b 100644 --- a/frontend/src/app/components/mapping/spiderfy.tsx +++ b/frontend/src/app/components/mapping/spiderfy.tsx @@ -55,6 +55,14 @@ const Spiderfy = (props: SpiderfyProps) => { }); }, [oms, props]); + useEffect(() => { + return () => { + oms.clearListeners("spiderfy"); + oms.clearListeners("unspiderfy"); + oms.clearListeners("click"); + }; + }, [oms]); + useEffect(() => { map.eachLayer((layer) => { // Hacky, but "RiseOnHover" is being used to identify non-clustered markers that should be spiderfied.