question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support loading google maps api via a library

See original GitHub issue

Feature 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:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Joey-z-rpcommented, Feb 11, 2021

@wellyshen Legend! Thanks very much!

1reaction
Joey-z-rpcommented, Feb 11, 2021

@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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Overview | Maps JavaScript API - Google Developers
We load the Maps JavaScript API using a script tag. These steps are explained below. ... For more information, see Libraries in the...
Read more >
Loading google maps api with libraries - Stack Overflow
The JavaScript code for the Maps API is loaded via a bootstrap URL of the form http://maps.googleapis.com/maps/api/js.
Read more >
How to: load the Google Maps Places library ... - Robin's Blog
How to: load the Google Maps Places library through Google API Loader ... the list of supported APIs doesn't include the Google Maps...
Read more >
Loading Google Maps Platform JavaScript in Modern Web ...
The first library, @googlemaps/js-api-loader, dynamically loads the Maps JavaScript API and wraps the process in a Promise.
Read more >
Dynamically load map libraries · Issue #5 - GitHub
Hi all, This version of the map loader, like the previous one, ... with a library, I get the error about the API...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found