JustFly1984 / react-google-maps-api

React Google Maps API
MIT License
1.78k stars 437 forks source link

Allow useLoadScript skip initialization #3153

Open jakub-chatrny opened 1 year ago

jakub-chatrny commented 1 year ago

Please provide an explanation of the issue

In our case googleMapsApiKey is provided by our server API. So we need to skip initialization until config data are loaded. That can be kinda tricky, since early return in body of hook or conditional call of other hooks is not recomended in hooks. Also in api of useLoadScript hook is googleMapsApiKey marked as required so we need to ts ignore there

Proposed solution

  1. One way to solve this is make googleMapsApiKey and add check if is nullish here
  2. Other solution is leave it required, but add another attribute skip: boolean, which will early return in useEffect also here. In our case we would provide googleMapsApiKey: "", skip: true. Which is kinda hacky but doesn't require change of api (just addition).

I can provide PR, which solution would you prefer? Is there any other way you can think of?