Skip to content

Commit 7d7992b

Browse files
committed
Fix map zoom reset by preventing unnecessary reinitialization
- Remove initialZoom from useEffect dependency array to prevent reinitialization on zoom changes - Use useRef to store initial zoom value to avoid reactive updates - Prevents map recentering when user zooms and savedZoom state updates - Map now maintains user's zoom level and position during interactions
1 parent 7c278f8 commit 7d7992b

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

app/components/map/MapPicker.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ const MapPicker: React.FC<MapPickerProps> = ({
4848
const [error, setError] = useState<string | null>(null);
4949
const { resolvedTheme } = useTheme();
5050

51+
// Store initial zoom in a ref so it doesn't cause re-renders
52+
const initialZoomRef = useRef(initialZoom);
53+
5154
// Initialize map
5255
useEffect(() => {
5356
const initializeMap = async () => {
@@ -103,12 +106,12 @@ const MapPicker: React.FC<MapPickerProps> = ({
103106
// Use the location's coordinates and zoom
104107
centerLat = location.lat;
105108
centerLng = location.lng;
106-
zoom = initialZoom || location.zoom || 15;
109+
zoom = initialZoomRef.current || location.zoom || 15;
107110
} else {
108111
// Default to North America view
109112
centerLat = 45.0;
110113
centerLng = -100.0;
111-
zoom = initialZoom || 2;
114+
zoom = initialZoomRef.current || 2;
112115
}
113116

114117
map.setView([centerLat, centerLng], zoom);
@@ -204,7 +207,7 @@ const MapPicker: React.FC<MapPickerProps> = ({
204207
markerRef.current = null;
205208
}
206209
};
207-
}, [initialZoom, disableZoom, allowPanning, resolvedTheme, readOnly, onChange, onZoomChange]); // Removed 'location' to prevent map reinitialization on location changes
210+
}, [disableZoom, allowPanning, resolvedTheme, readOnly, onChange, onZoomChange]); // Removed 'location' and 'initialZoom' to prevent map reinitialization
208211

209212
// Handle location changes - only update marker, not map view
210213
useEffect(() => {

0 commit comments

Comments
 (0)