Support loading google maps api via a library
See original GitHub issueFeature Request
Describe the Feature
Thanks for the awesome work.
We are using load-google-maps-api
to load google maps api with useEffect when the component loads. Because there is no script that we can set callback
name and useEffect
runs after component loads, the ready
state returned by usePlacesAutocomplete
is always false and it stays that way until the component is “remounted” (e.g navigate away and back). The workaround we found is setting a fake callback name as a state and pass it to usePlacesAutocomplete
, once the load google maps promise resolves we set the callback name to undefined
, which triggers the effect calling init
function in use-places-autocomplete
(because callbackName
is one of the dependencies of that effect). This works fine but it’s coupled to the library’s internal implementation. Is there a better way to solve this or could you add support for loading google maps api via third party library?
Describe the Solution You’d Like
I think if the usePlacesAutocomplete
hook could expose the init
function for users to call it whenever the google maps api is loaded, the above issue would be solved.
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (3 by maintainers)
Top GitHub Comments
@wellyshen Legend! Thanks very much!
@wellyshen Thanks for the quick reply. However, the solution mentioned doesn’t seem to be a good fit for us because we want to display the same input field regardless of whether the API is loaded or not. To achieve that with the said solution, we would need to duplicate the input field, which isn’t very clean. Also the solution essentially renders the ready state useless because we will never use it before it’s ready. The root problem here is that there is no way for users to notify the library that the API is loaded, apart from the callback name which only supports script loading. This is why I think exposing a method for users to notify that the API is loaded would be a good addition.