Skip to content

Commit 43954d5

Browse files
committed
fix: improve prop handling in MlWmsLoader to prevent reinitialization
1 parent 3d6a9fd commit 43954d5

File tree

2 files changed

+14
-18
lines changed

2 files changed

+14
-18
lines changed

packages/react-maplibre/src/components/MlWmsLoader/MlWmsLoader.stories.tsx

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -175,18 +175,6 @@ const CustomFeatureInfoTemplate: any = () => {
175175
content: string;
176176
lngLat: { lng: number; lat: number };
177177
} | null>(null);
178-
const mapHook = useMap({
179-
mapId: undefined,
180-
});
181-
182-
const initializedRef = useRef(false);
183-
184-
useEffect(() => {
185-
if (!mapHook.map || initializedRef.current) return;
186-
187-
initializedRef.current = true;
188-
mapHook.map.map.flyTo({ center: [2.3522, 48.8566], zoom: 12 });
189-
}, [mapHook.map]);
190178

191179
const handleFeatureInfoSuccess = (content: string, lngLat: { lng: number; lat: number }) => {
192180
setFeatureInfoData({ content, lngLat });

packages/react-maplibre/src/components/MlWmsLoader/MlWmsLoader.tsx

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -211,23 +211,31 @@ const defaultGetFeatureInfoUrlParameters = {
211211
*/
212212
const MlWmsLoader = (props: MlWmsLoaderProps) => {
213213
// Merge defaults with props using useMemo for stable references
214-
// The dependencies are the prop objects - if consumers pass inline objects,
215-
// they should memoize them. This follows standard React patterns.
214+
// Use JSON.stringify for stable dependency comparison to prevent reinitialization
215+
// when consumers pass inline objects as props
216+
const baseUrlParametersJson = JSON.stringify(props.baseUrlParameters);
216217
const baseUrlParameters = useMemo(
217218
() => ({ ...defaultBaseUrlParameters, ...props.baseUrlParameters }),
218-
[props.baseUrlParameters]
219+
// eslint-disable-next-line react-hooks/exhaustive-deps
220+
[baseUrlParametersJson]
219221
);
222+
const getCapabilitiesUrlParametersJson = JSON.stringify(props.getCapabilitiesUrlParameters);
220223
const getCapabilitiesUrlParameters = useMemo(
221224
() => ({ ...defaultGetCapabilitiesUrlParameters, ...props.getCapabilitiesUrlParameters }),
222-
[props.getCapabilitiesUrlParameters]
225+
// eslint-disable-next-line react-hooks/exhaustive-deps
226+
[getCapabilitiesUrlParametersJson]
223227
);
228+
const getMapUrlParametersJson = JSON.stringify(props.getMapUrlParameters);
224229
const getMapUrlParameters = useMemo(
225230
() => ({ ...defaultGetMapUrlParameters, ...props.getMapUrlParameters }),
226-
[props.getMapUrlParameters]
231+
// eslint-disable-next-line react-hooks/exhaustive-deps
232+
[getMapUrlParametersJson]
227233
);
234+
const getFeatureInfoUrlParametersJson = JSON.stringify(props.getFeatureInfoUrlParameters);
228235
const getFeatureInfoUrlParameters = useMemo(
229236
() => ({ ...defaultGetFeatureInfoUrlParameters, ...props.getFeatureInfoUrlParameters }),
230-
[props.getFeatureInfoUrlParameters]
237+
// eslint-disable-next-line react-hooks/exhaustive-deps
238+
[getFeatureInfoUrlParametersJson]
231239
);
232240

233241
const featureInfoSuccessRef = useRef(props.featureInfoSuccess);

0 commit comments

Comments
 (0)