Skip to content

Commit

Permalink
WIP: dynamically import MapGL
Browse files Browse the repository at this point in the history
  • Loading branch information
tuukka committed May 27, 2020
1 parent 304a8ac commit 03d2253
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import React, { useState, useEffect, useRef, ReactText } from "react";
import React, { useState, useEffect, useRef, ReactText, Suspense } from "react";
import { useRouteMatch, useHistory } from "react-router-dom";
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import type { match } from "react-router-dom";
import { Button, IconButton } from "@material-ui/core";
import { Close as CloseIcon } from "@material-ui/icons";
import { useSnackbar } from "notistack";
import MapGL, { Popup, Source, Layer, Marker } from "@urbica/react-map-gl";
import { WebMercatorViewport } from "viewport-mercator-project";
import type { WebMercatorViewportOptions } from "viewport-mercator-project";
import { distance as turfDistance } from "@turf/turf";
Expand Down Expand Up @@ -136,6 +135,12 @@ const fitBounds = (
};

const App: React.FC = () => {
const MapGL = React.lazy(() => import("@urbica/react-map-gl"));
const Popup = React.lazy(async () => {return (await import("@urbica/react-map-gl")).Popup});
const Source = React.lazy(async () => {return (await import("@urbica/react-map-gl")).Source});
const Layer = React.lazy(async () => {return (await import("@urbica/react-map-gl")).Layer});
const Marker = React.lazy(async () => {return (await import("@urbica/react-map-gl")).Marker});

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const map = useRef<any>(null);

Expand Down Expand Up @@ -489,7 +494,8 @@ const App: React.FC = () => {
);
}}
/>
<MapGL
<Suspense fallback={<div>loading</div>}>
<MapGL
ref={map}
// This is according to the Get Started materials:
// https://uber.github.io/react-map-gl/docs/get-started/get-started/
Expand Down Expand Up @@ -707,6 +713,7 @@ const App: React.FC = () => {
</Popup>
)}
</MapGL>
</Suspense>
</div>
);
};
Expand Down

0 comments on commit 03d2253

Please sign in to comment.