Skip to main content

useMapsLibrary Hook

React hook to get access to the different Google Maps API libraries. This is essentially a react-version of the google.maps.importLibrary function.

const MyComponent = () => {
const map = useMap();
const placesLib = useMapsLibrary('places');

useEffect(() => {
if (!placesLib || !map) return;

const svc = new placesLib.PlacesService(map);
// ...
}, [placesLib, map]);

// ...
};

Signature

useMapsLibrary(name: string): google.maps.XxxLibrary

Returns the library object as it is returned by google.maps.importLibrary.

Parameters

name: string (required)

The name of the library that should be loaded

Source

src/hooks/use-maps-library.ts