React component that wraps the loading of Google Maps JavaScript API.
npm install @googlemaps/react-wrapper
!Build
!Release

!GitHub contributors


> [!IMPORTANT]
> This library has been archived.
> We recommend all users of this package to switch to the new
> @vis.gl/react-google-maps,
> which provides a collection of components and hooks and can be configured
> to be fully compatible with this package.
>
> See the migration guide
> for more details on how to switch from this package to @vis.gl/react-google-maps.
Wrap React components with this library to load the Google Maps JavaScript API.
``jsx
import {Wrapper} from '@googlemaps/react-wrapper';
const MyApp = () => (
);
`
The preceding example will not render any elements unless the Google Maps JavaScript API is successfully loaded. To handle error cases and the time until load is complete, it is recommended to provide render props.
`jsx
import {Wrapper, Status} from '@googlemaps/react-wrapper';
const render = status => {
switch (status) {
case Status.LOADING:
return
case Status.FAILURE:
return
case Status.SUCCESS:
return
}
};
const MyApp = () =>
`
When combining children and render props, the children will render on success and the render prop will be executed for other status values.
`tsx
import {Wrapper, Status} from '@googlemaps/react-wrapper';
const render = (status: Status): ReactElement => {
if (status === Status.FAILURE) return
return
};
const MyApp = () => (
);
`
This wrapper uses [@googlemaps/js-api-loader][js_api_loader] to load the Google Maps JavaScript API. This library uses a singleton pattern and will not attempt to load the library more than once. All options accepted by [@googlemaps/js-api-loader][js_api_loader] are also accepted as props to the wrapper component.
The following snippets demonstrates the usage of useRef and useEffect hooks with Google Maps.
`tsx
function MyMapComponent({
center,
zoom,
}: {
center: google.maps.LatLngLiteral;
zoom: number;
}) {
const ref = useRef
const [map, setMap] = useState
useEffect(() => {
const map = new google.maps.Map(ref.current, {center, zoom});
setMap(map);
return () => {
google.maps.event.clearInstanceListeners(map);
setMap(null);
};
}, []);
useEffect(() => {
if (!map) return;
// do something with the map instance
}, [map]);
return
;Examples
See the examples folder for additional usage patterns.
Install
Available via npm as the package @googlemaps/react-wrapper.
`sh
npm i @googlemaps/react-wrapper
`or
`sh
yarn add @googlemaps/react-wrapper
`For TypeScript support additionally install type definitions.
`sh
npm i -D @types/google.maps
`or
`sh
yarn add -D @types/google.maps
``The reference documentation can be found at this link.
This library is community supported. We're comfortable enough with the stability and features of
the library that we want you to build real production applications on it.
If you find a bug, or have a feature suggestion, please [log an issue][issues]. If you'd like to
contribute, please read [How to Contribute][contrib].
[issues]: https://github.com/googlemaps/react-wrapper/issues
[contrib]: https://github.com/googlemaps/react-wrapper/blob/master/CONTRIBUTING.md
[js_api_loader]: https://www.npmjs.com/package/@googlemaps/js-api-loader