visgl / react-google-maps

React components and hooks for the Google Maps JavaScript API
https://visgl.github.io/react-google-maps/
MIT License
1.29k stars 108 forks source link

[Bug] Event listener on map initialized by defaultBounds causes warning in console #549

Open mfavetti opened 1 month ago

mfavetti commented 1 month ago

Description

Warning in console when initializing a map with defaultBounds and adding an event listener:

main.js:169 [createEvent] at least one of the values from the map returned undefined. This is not expected to happen. Please report an issue at https://github.com/visgl/react-google-maps/issues/new

Steps to Reproduce

import React from 'react';
import {
  APIProvider,
  Map,
} from '@vis.gl/react-google-maps';

import './App.css';

const API_KEY = '';
const MAP_ID = '';

export default function MyApp() {
  return (
    <APIProvider apiKey={API_KEY}>
      <Map
        id="map"
        mapId={MAP_ID}
        defaultBounds={{north: 45, south: 0, east: 0, west: 45}}
        onCenterChanged={() => {console.log('center changed')}}
      />
    </APIProvider>
  );
}

Environment

Logs

main.js:169 [createEvent] at least one of the values from the map returned undefined. This is not expected to happen. Please report an issue at https://github.com/visgl/react-google-maps/issues/new
createMapEvent  @   use-map-events.ts:104
(anonymous) @   use-map-events.ts:66
_.nk    @   main.js:169
tk  @   main.js:170
_.rk.set    @   main.js:294
(anonymous) @   main.js:177
d   @   map.js:23
iC  @   map.js:24
(anonymous) @   main.js:414
Promise.then        
fn.fitBounds    @   main.js:414
(anonymous) @   use-map-instance.ts:157
commitHookEffectListMount   @   react-dom.development.js:23189
commitPassiveMountOnFiber   @   react-dom.development.js:24965
commitPassiveMountEffects_complete  @   react-dom.development.js:24930
commitPassiveMountEffects_begin @   react-dom.development.js:24917
commitPassiveMountEffects   @   react-dom.development.js:24905
flushPassiveEffectsImpl @   react-dom.development.js:27078
flushPassiveEffects @   react-dom.development.js:27023
(anonymous) @   react-dom.development.js:26808
workLoop    @   scheduler.development.js:266
flushWork   @   scheduler.development.js:239
performWorkUntilDeadline    @   scheduler.development.js:533
Show less
mfavetti commented 1 month ago

This prevents the warning:

<Map
        id="map"
        mapId={MAP_ID}
        defaultBounds={{north: 45, south: 0, east: 0, west: 45}}
        onCenterChanged={() => {console.log('center changed')}}
        defaultCenter={{lat: 0, lng: 0}}
        defaultZoom={1}
      />
mfavetti commented 1 month ago

I tried some other events and so far onCenterChanged is the only one I can reproduce with.

usefulthink commented 1 month ago

Thanks! The official documentation doesn't have any information on why the types for map.center etc are specified as "might be undefined" and in about ten years with the maps API, I never encountered a situation where they were in fact undefined. That is why I added that error-message so I can finally learn in which situation that will happen :)

I'll figure out how we can prevent that from being a problem...