Skip to content

Commit b40a984

Browse files
committed
feat: Use @googlemaps/js-api-loader
1 parent 39c7817 commit b40a984

File tree

3 files changed

+53
-32
lines changed

3 files changed

+53
-32
lines changed

package-lock.json

Lines changed: 26 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"@babel/preset-react": "^7.16.0",
3838
"@emotion/react": "^11.11.3",
3939
"@emotion/styled": "^11.11.0",
40+
"@googlemaps/js-api-loader": "^1.16.6",
4041
"@mui/icons-material": "^5.15.3",
4142
"@mui/material": "^5.15.3",
4243
"autosuggest-highlight": "^3.3.4",
@@ -50,7 +51,7 @@
5051
"@babel/eslint-parser": "^7.17.0",
5152
"@babel/preset-typescript": "^7.16.7",
5253
"@types/autosuggest-highlight": "^3.2.0",
53-
"@types/google.maps": "^3.48.5",
54+
"@types/google.maps": "^3.55.9",
5455
"@types/lodash.throttle": "^4.1.7",
5556
"@typescript-eslint/eslint-plugin": "^5.18.0",
5657
"@typescript-eslint/parser": "^5.18.0",

src/AddressAutocomplete.tsx

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import parse from 'autosuggest-highlight/parse';
44
import throttle from 'lodash.throttle';
55
import React, { useEffect, useMemo, useRef, useState } from 'react';
66
import { AddressAutocompleteProps, AddressAutocompleteValue, PlaceType } from '../dist/AddressAutocomplete';
7+
import { Loader } from '@googlemaps/js-api-loader';
78

89
type AutocompleteServiceHolder = {
910
current: google.maps.places.AutocompleteService | null
@@ -30,6 +31,7 @@ const AddressAutocomplete = ({
3031
const [addressOptions, setAddressOptions] = useState<readonly PlaceType[]>([]);
3132
const [addressValue, setAddressValue] = useState<AddressAutocompleteValue | null>(value);
3233
const [addressInputValue, setAddressInputValue] = useState('');
34+
const [google, setGoogle] = useState<google.maps.PlacesLibrary | null>(null);
3335

3436
// Update inner value when props value change
3537
useEffect(() => {
@@ -150,25 +152,26 @@ const AddressAutocomplete = ({
150152
);
151153
};
152154

153-
// Load Google Maps API script if not already loaded
155+
// Load Google Maps API if not already loaded
154156
useEffect(() => {
155-
if (typeof window !== 'undefined' && !loaded.current) {
156-
if (!document.querySelector('#google-maps')) {
157-
const script = document.createElement('script');
157+
if (google) return;
158158

159-
if (!apiKey) {
160-
console.error('You need to provide an API key to use this component');
161-
}
162-
163-
script.setAttribute('async', '');
164-
script.setAttribute('id', 'google-maps');
165-
script.src = `https://maps.googleapis.com/maps/api/js?key=${apiKey}&libraries=places`;
166-
document.querySelector('head')?.appendChild(script);
167-
}
168-
169-
loaded.current = true;
159+
if (!apiKey) {
160+
console.error('You need to provide an API key to use this component');
161+
return;
170162
}
171-
}, [apiKey, loaded]);
163+
164+
const loader = new Loader({
165+
apiKey,
166+
version: 'weekly',
167+
});
168+
169+
loader.importLibrary('places').then((google) => {
170+
setGoogle(google);
171+
}).catch((err) => {
172+
console.error(err);
173+
});
174+
}, [apiKey]);
172175

173176
// Autocomplete predictions fetcher
174177
const fetch = useMemo(() => throttle((
@@ -185,13 +188,15 @@ const AddressAutocomplete = ({
185188
// Lock worker
186189
let active = true;
187190

191+
if (!google) return;
192+
188193
// Initialize Google Maps Autocomplete Service
189-
if (!autocompleteService.current && window.google) {
190-
autocompleteService.current = new window.google.maps.places.AutocompleteService();
194+
if (!autocompleteService.current) {
195+
autocompleteService.current = new google.AutocompleteService();
191196
}
192197
// Initialize Google Maps Places Service
193-
if (!placesService.current && window.google) {
194-
placesService.current = new window.google.maps.places.PlacesService(document.createElement('div'));
198+
if (!placesService.current) {
199+
placesService.current = new google.PlacesService(document.createElement('div'));
195200
}
196201
// Stop execution if the service is not available
197202
if (!autocompleteService.current || !placesService.current) {

0 commit comments

Comments
 (0)