@revivejs/react-google-maps 19.0.0 → 19.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -16,7 +16,7 @@ function useGoogleMapsApiContext() {
16
16
  }
17
17
 
18
18
  // src/loadGoogleMapsApi.ts
19
- var DEFAULT_LIBRARIES = ["marker", "places", "geometry", "visualization"];
19
+ var DEFAULT_LIBRARIES = [];
20
20
  var loaderPromise = null;
21
21
  var loadedOptionsKey = null;
22
22
  function getDefaultGoogleMapsLibraries() {
@@ -26,9 +26,6 @@ function loadGoogleMapsApi(options) {
26
26
  if (typeof window === "undefined" || typeof document === "undefined") {
27
27
  return Promise.reject(new Error("@revivejs/react-google-maps can only load the Google Maps API in a browser environment."));
28
28
  }
29
- if (!options.apiKey) {
30
- return Promise.reject(new Error("A Google Maps API key is required to load the Google Maps JavaScript API."));
31
- }
32
29
  if (window.google?.maps) {
33
30
  return Promise.resolve(window.google);
34
31
  }
@@ -58,7 +55,7 @@ function loadGoogleMapsApi(options) {
58
55
  return;
59
56
  }
60
57
  const params = new URLSearchParams();
61
- params.set("key", normalizedOptions.apiKey);
58
+ params.set("key", normalizedOptions.apiKey ?? "");
62
59
  params.set("v", normalizedOptions.version);
63
60
  params.set("loading", "async");
64
61
  params.set("callback", callbackName);
@@ -133,12 +130,6 @@ function GoogleMapsProvider({ children, ...options }) {
133
130
  });
134
131
  useEffect(() => {
135
132
  let cancelled = false;
136
- if (!options.apiKey) {
137
- setStatus("idle");
138
- setError(null);
139
- setGoogleApi(null);
140
- return;
141
- }
142
133
  setStatus((current) => current === "ready" ? current : "loading");
143
134
  setError(null);
144
135
  loadGoogleMapsApi(options).then((googleNamespace) => {
@@ -378,8 +369,13 @@ var GoogleMap = forwardRef(function GoogleMap2({
378
369
  }, ref) {
379
370
  const { isLoaded, status, error, google: google2 } = useGoogleMapsApi();
380
371
  const containerRef = useRef2(null);
372
+ const mapRef = useRef2(null);
373
+ const onMapLoadRef = useRef2(onMapLoad);
374
+ const onMapUnmountRef = useRef2(onMapUnmount);
381
375
  const [map, setMap] = useState2(null);
382
376
  const initialMapIdRef = useRef2(mapId);
377
+ onMapLoadRef.current = onMapLoad;
378
+ onMapUnmountRef.current = onMapUnmount;
383
379
  useImperativeHandle(
384
380
  ref,
385
381
  () => ({
@@ -448,7 +444,7 @@ var GoogleMap = forwardRef(function GoogleMap2({
448
444
  [map]
449
445
  );
450
446
  useEffect2(() => {
451
- if (!isLoaded || !google2 || !containerRef.current || map) {
447
+ if (!isLoaded || !google2 || !containerRef.current || mapRef.current) {
452
448
  return;
453
449
  }
454
450
  const nextMap = new google2.maps.Map(
@@ -459,13 +455,16 @@ var GoogleMap = forwardRef(function GoogleMap2({
459
455
  mapId: initialMapIdRef.current
460
456
  })
461
457
  );
458
+ mapRef.current = nextMap;
462
459
  setMap(nextMap);
463
- onMapLoad?.(nextMap);
460
+ onMapLoadRef.current?.(nextMap);
464
461
  return () => {
465
- onMapUnmount?.(nextMap);
462
+ google2.maps.event.clearInstanceListeners(nextMap);
463
+ onMapUnmountRef.current?.(nextMap);
464
+ mapRef.current = null;
466
465
  setMap(null);
467
466
  };
468
- }, [isLoaded, google2, map]);
467
+ }, [isLoaded, google2]);
469
468
  useEffect2(() => {
470
469
  if (!map) {
471
470
  return;
@@ -634,7 +633,12 @@ var MapMarker = forwardRef2(function MapMarker2({
634
633
  }, ref) {
635
634
  const map = useGoogleMap();
636
635
  const clustererContext = useContext3(MarkerClustererContext);
636
+ const markerRef = useRef3(null);
637
+ const onLoadRef = useRef3(onLoad);
638
+ const onUnmountRef = useRef3(onUnmount);
637
639
  const [marker, setMarker] = useState3(null);
640
+ onLoadRef.current = onLoad;
641
+ onUnmountRef.current = onUnmount;
638
642
  useImperativeHandle2(
639
643
  ref,
640
644
  () => ({
@@ -688,18 +692,22 @@ var MapMarker = forwardRef2(function MapMarker2({
688
692
  [marker]
689
693
  );
690
694
  useEffect3(() => {
691
- if (!map || marker) {
695
+ if (!map || markerRef.current) {
692
696
  return;
693
697
  }
694
698
  const instance = new google.maps.Marker();
699
+ markerRef.current = instance;
695
700
  setMarker(instance);
696
- onLoad?.(instance);
701
+ onLoadRef.current?.(instance);
697
702
  return () => {
698
- onUnmount?.(instance);
703
+ google.maps.event.clearInstanceListeners(instance);
704
+ onUnmountRef.current?.(instance);
699
705
  clustererContext?.unregisterMarker(instance);
700
706
  instance.setMap(null);
707
+ markerRef.current = null;
708
+ setMarker(null);
701
709
  };
702
- }, [map, marker]);
710
+ }, [map, clustererContext]);
703
711
  useEffect3(() => {
704
712
  if (!marker || !map) {
705
713
  return;