Skip to content

Commit 03d2253

Browse files
committed
WIP: dynamically import MapGL
1 parent 304a8ac commit 03d2253

1 file changed

Lines changed: 10 additions & 3 deletions

File tree

src/App.tsx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
import React, { useState, useEffect, useRef, ReactText } from "react";
1+
import React, { useState, useEffect, useRef, ReactText, Suspense } from "react";
22
import { useRouteMatch, useHistory } from "react-router-dom";
33
// eslint-disable-next-line @typescript-eslint/no-unused-vars
44
import type { match } from "react-router-dom";
55
import { Button, IconButton } from "@material-ui/core";
66
import { Close as CloseIcon } from "@material-ui/icons";
77
import { useSnackbar } from "notistack";
8-
import MapGL, { Popup, Source, Layer, Marker } from "@urbica/react-map-gl";
98
import { WebMercatorViewport } from "viewport-mercator-project";
109
import type { WebMercatorViewportOptions } from "viewport-mercator-project";
1110
import { distance as turfDistance } from "@turf/turf";
@@ -136,6 +135,12 @@ const fitBounds = (
136135
};
137136

138137
const App: React.FC = () => {
138+
const MapGL = React.lazy(() => import("@urbica/react-map-gl"));
139+
const Popup = React.lazy(async () => {return (await import("@urbica/react-map-gl")).Popup});
140+
const Source = React.lazy(async () => {return (await import("@urbica/react-map-gl")).Source});
141+
const Layer = React.lazy(async () => {return (await import("@urbica/react-map-gl")).Layer});
142+
const Marker = React.lazy(async () => {return (await import("@urbica/react-map-gl")).Marker});
143+
139144
// eslint-disable-next-line @typescript-eslint/no-explicit-any
140145
const map = useRef<any>(null);
141146

@@ -489,7 +494,8 @@ const App: React.FC = () => {
489494
);
490495
}}
491496
/>
492-
<MapGL
497+
<Suspense fallback={<div>loading</div>}>
498+
<MapGL
493499
ref={map}
494500
// This is according to the Get Started materials:
495501
// https://uber.github.io/react-map-gl/docs/get-started/get-started/
@@ -707,6 +713,7 @@ const App: React.FC = () => {
707713
</Popup>
708714
)}
709715
</MapGL>
716+
</Suspense>
710717
</div>
711718
);
712719
};

0 commit comments

Comments
 (0)