MCPcopy Create free account
hub / github.com/visgl/react-map-gl / _Map

Function _Map

modules/react-mapbox/src/components/map.tsx:37–134  ·  view source on GitHub ↗
(props: MapProps, ref: React.Ref<MapRef>)

Source from the content-addressed store, hash-verified

35 };
36
37function _Map(props: MapProps, ref: React.Ref<MapRef>) {
38 const mountedMapsContext = useContext(MountedMapsContext);
39 const [mapInstance, setMapInstance] = useState<Mapbox>(null);
40 const containerRef = useRef();
41
42 const {current: contextValue} = useRef<MapContextValue>({mapLib: null, map: null});
43
44 useEffect(() => {
45 const mapLib = props.mapLib;
46 let isMounted = true;
47 let mapbox: Mapbox;
48
49 Promise.resolve(mapLib || import('mapbox-gl'))
50 .then((module: MapLib | {default: MapLib}) => {
51 if (!isMounted) {
52 return;
53 }
54 if (!module) {
55 throw new Error('Invalid mapLib');
56 }
57 const mapboxgl = 'Map' in module ? module : module.default;
58 if (!mapboxgl.Map) {
59 throw new Error('Invalid mapLib');
60 }
61
62 setGlobals(mapboxgl, props);
63 if (props.reuseMaps) {
64 mapbox = Mapbox.reuse(props, containerRef.current);
65 }
66 if (!mapbox) {
67 mapbox = new Mapbox(mapboxgl.Map, props, containerRef.current);
68 }
69 contextValue.map = createRef(mapbox);
70 contextValue.mapLib = mapboxgl;
71
72 setMapInstance(mapbox);
73 mountedMapsContext?.onMapMount(contextValue.map, props.id);
74 })
75 .catch(error => {
76 const {onError} = props;
77 if (onError) {
78 onError({
79 type: 'error',
80 target: null,
81 error
82 });
83 } else {
84 console.error(error); // eslint-disable-line
85 }
86 });
87
88 return () => {
89 isMounted = false;
90 if (mapbox) {
91 mountedMapsContext?.onMapUnmount(props.id);
92 if (props.reuseMaps) {
93 mapbox.recycle();
94 } else {

Callers

nothing calls this directly

Calls 7

setGlobalsFunction · 0.50
createRefFunction · 0.50
onErrorFunction · 0.50
reuseMethod · 0.45
recycleMethod · 0.45
destroyMethod · 0.45
setPropsMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…