Skip to content

Commit 51563de

Browse files
iobuhovclaude
andcommitted
refactor(maps-web): use React 18 useEffect pattern for LeafletMap ref cleanup
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent 3b4d398 commit 51563de

1 file changed

Lines changed: 14 additions & 19 deletions

File tree

packages/pluggableWidgets/maps-web/src/components/LeafletMap.tsx

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import classNames from "classnames";
2-
import { ReactElement, useCallback, useRef } from "react";
2+
import { ReactElement, useEffect, useRef } from "react";
33
import { getDimensions } from "@mendix/widget-plugin-platform/utils/get-dimensions";
44
import { MapProviderEnum } from "../../typings/MapsProps";
55
import { SharedProps } from "../../typings/shared";
@@ -12,32 +12,27 @@ export interface LeafletProps extends SharedProps {
1212

1313
export function LeafletMap(props: LeafletProps): ReactElement {
1414
const vm = useLeafletMapVM();
15-
const cleanupRef = useRef<(() => void) | undefined>(undefined);
15+
const mapNodeRef = useRef<HTMLDivElement | null>(null);
1616

17-
const refCallback = useCallback(
18-
(node: HTMLDivElement | null) => {
19-
cleanupRef.current?.();
20-
cleanupRef.current = undefined;
17+
useEffect(() => {
18+
const node = mapNodeRef.current;
19+
if (!node) {
20+
return;
21+
}
2122

22-
if (node) {
23-
cleanupRef.current = vm.setupMap(node);
24-
// React 19: returned cleanup is called on unmount.
25-
// React 18: ignored (cleanup happens via null-call above).
26-
return () => {
27-
cleanupRef.current?.();
28-
cleanupRef.current = undefined;
29-
};
30-
}
31-
},
32-
[vm]
33-
);
23+
const cleanup = vm.setupMap(node);
24+
25+
return () => {
26+
cleanup();
27+
};
28+
}, [vm]);
3429

3530
return (
3631
<div className={classNames("widget-maps", props.className)} style={{ ...props.style, ...getDimensions(props) }}>
3732
<div className="widget-leaflet-maps-wrapper">
3833
<div
3934
className="widget-leaflet-maps"
40-
ref={refCallback}
35+
ref={mapNodeRef}
4136
style={{ top: 0, bottom: 0, left: 0, right: 0, position: "absolute", zIndex: 1 }}
4237
/>
4338
</div>

0 commit comments

Comments
 (0)