11import classNames from "classnames" ;
2- import { ReactElement , useCallback , useRef } from "react" ;
2+ import { ReactElement , useEffect , useRef } from "react" ;
33import { getDimensions } from "@mendix/widget-plugin-platform/utils/get-dimensions" ;
44import { MapProviderEnum } from "../../typings/MapsProps" ;
55import { SharedProps } from "../../typings/shared" ;
@@ -12,32 +12,27 @@ export interface LeafletProps extends SharedProps {
1212
1313export 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