Skip to content

Instantly share code, notes, and snippets.

@JenniferFuBook
Created February 21, 2023 03:19
Show Gist options
  • Save JenniferFuBook/586c60b3392d6ca08dbb43f235804141 to your computer and use it in GitHub Desktop.
Save JenniferFuBook/586c60b3392d6ca08dbb43f235804141 to your computer and use it in GitHub Desktop.
import { useEffect, useRef } from 'react';
import L from 'leaflet';
import 'leaflet/dist/leaflet.css';
function App() {
const savedMap = useRef();
const mapId = 'map';
useEffect(() => {
if (savedMap.current) {
savedMap.current.off();
savedMap.current.remove();
}
const map = L.map(mapId, {
center: [37.774929, -122.419418],
zoom: 8,
});
savedMap.current = map;
L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}{r}.png', {
attribution:
'&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors',
}).addTo(map);
}, []);
return <div id={mapId}></div>;
}
export default App;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment