-
-
Save tstrohmeier/d3c41322e33095c83f23dbc9401c903d to your computer and use it in GitHub Desktop.
When adding Google Maps to your react application using the library @react-google-maps/api, you run into issues when having multiple components loading a map. In this gist you'll find a component and a hook to resolve these issues and to simplify integrating the library in your application.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { FC } from 'react'; | |
import { GoogleMap } from '@react-google-maps/api'; | |
import { useGoogleMaps } from './GoogleMapsProvider'; | |
const containerStyle = { | |
width: '400px', | |
height: '400px' | |
}; | |
const center = { | |
lat: -3.745, | |
lng: -38.523 | |
}; | |
const App: FC = () => { | |
const { isLoaded } = useGoogleMaps(); | |
return isLoaded | |
? ( | |
<GoogleMap | |
mapContainerStyle={containerStyle} | |
center={center} | |
zoom={10} | |
/> | |
) | |
: ( | |
<div>Loading...</div> | |
); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { createContext, FC, useContext } from 'react'; | |
import { useJsApiLoader } from '@react-google-maps/api'; | |
import { UseLoadScriptOptions } from '@react-google-maps/api/dist/useJsApiLoader'; | |
export type GoogleMapsState = { | |
isLoaded: boolean; | |
loadError?: Error; | |
}; | |
const GoogleMapsContext = createContext<GoogleMapsState>({ isLoaded: false }); | |
export const GoogleMapsProvider: FC<UseLoadScriptOptions> = ({ children, ...loadScriptOptions }) => { | |
const { isLoaded, loadError } = useJsApiLoader(loadScriptOptions); | |
return ( | |
<GoogleMapsContext.Provider value={{ isLoaded, loadError }}> | |
{children} | |
</GoogleMapsContext.Provider> | |
); | |
}; | |
export const useGoogleMaps = () => useContext(GoogleMapsContext); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Libraries } from "@react-google-maps/api/dist/utils/make-load-script-url"; | |
import React from 'react'; | |
import ReactDOM from 'react-dom'; | |
import { Provider } from 'react-redux'; | |
import { BrowserRouter as Router } from "react-router-dom"; | |
import App from 'app'; | |
import store from 'state/store'; | |
import { GoogleMapsProvider } from './GoogleMapsProvider'; | |
const googleMapsApiKey = 'abracadabra'; | |
const googleMapsLibraries: Libraries = [ 'places' ]; | |
ReactDOM.render( | |
<React.StrictMode> | |
<Router> | |
<Provider store={store}> | |
<GoogleMapsProvider googleMapsApiKey={googleMapsApiKey} language="en" libraries={googleMapsLibraries}> | |
<App /> | |
</GoogleMapsProvider> | |
</Provider> | |
</Router> | |
</React.StrictMode>, | |
document.getElementById('root') | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment