Skip to content

Instantly share code, notes, and snippets.

@sadewole
Last active July 6, 2021 23:37
Show Gist options
  • Save sadewole/b8f271af26092932797bf1f9adb0df1c to your computer and use it in GitHub Desktop.
Save sadewole/b8f271af26092932797bf1f9adb0df1c to your computer and use it in GitHub Desktop.
Load map
const map = useRef(null);
const mapContainerRef = useRef(null);
mapboxgl.accessToken = process.env.REACT_APP_MAPBOX_API; // your mapbox api
useEffect(() => {
if (map.current) return; // Checks if there's an already existing map initialised.
map.current = new mapboxgl.Map({
container: mapContainerRef.current,
style: 'mapbox://styles/mapbox/streets-v11',
zoom: 9,
center: [3.361881, 6.672557],
});
// clean up on unmount
return () => map.current.remove();
}, [])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment