Closed
Description
I am encountering an issue with Google maps in React. The steps that are causing the above error are as follows:
- A modal is opened on a specific page displaying a map with a marker on it
- The user clicks a button and they are navigated away from the page to another
- The error occurs during/ after navigation
this.Eg.close is not a function. (In 'this.Eg.close()', 'this.Eg.close' is undefined)
Environment details
- Either Marker or Maps API
- Mac OS X >=10.15.7
- v3
Code example
const Marker: FC<google.maps.marker.AdvancedMarkerElementOptions> = (options) => {
const [marker, setMarker] = useState<google.maps.marker.AdvancedMarkerElement>();
useEffect(() => {
if (marker === undefined && options.map !== undefined) {
setMarker(new window.google.maps.marker.AdvancedMarkerElement(options));
}
}, [marker, options.map]);
return null;
};
export const Map: FC<google.maps.MapOptions> = ({ center }) => {
const ref = useRef<HTMLDivElement>(null);
const [map, setMap] = useState<google.maps.Map>();
const [isMakerLibraryLoaded, setIsMarkerLibraryLoaded] = useState(false);
const isInitialized = isMakerLibraryLoaded;
useEffect(() => {
window.google.maps.importLibrary('marker').then(() => setIsMarkerLibraryLoaded(true));
}, []);
useEffect(() => {
if (ref.current !== null && map === undefined && isInitialized) {
setMap(
new window.google.maps.Map(ref.current, {
center,
zoom: 15,
streetViewControl: false,
mapTypeControl: false,
fullscreenControl: false,
mapId: 'avhasdasd',
})
);
}
}, [map, isInitialized]);
return (
<div
ref={ref}
style={{
width: '100%',
height: '200px',
marginTop: '20px',
position: 'absolute',
top: '-20px',
left: 0,
}}
>
{isInitialized && <Marker position={center} map={map} />}
</div>
);
};
Stack trace
this.Eg.close is not a function. (In 'this.Eg.close()', 'this.Eg.close' is undefined)
Metadata
Metadata
Assignees
Labels
No labels