@@ -4,6 +4,7 @@ import parse from 'autosuggest-highlight/parse';
44import throttle from 'lodash.throttle' ;
55import React , { useEffect , useMemo , useRef , useState } from 'react' ;
66import { AddressAutocompleteProps , AddressAutocompleteValue , PlaceType } from '../dist/AddressAutocomplete' ;
7+ import { Loader } from '@googlemaps/js-api-loader' ;
78
89type 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