Skip to content

Commit 0f9d68b

Browse files
authored
docs: update example in readme. (#854)
1 parent 5f26c70 commit 0f9d68b

1 file changed

Lines changed: 16 additions & 6 deletions

File tree

README.md

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,24 @@ function MyMapComponent({
8484
center: google.maps.LatLngLiteral;
8585
zoom: number;
8686
}) {
87-
const ref = useRef();
87+
const ref = useRef<HTMLDivElement>();
88+
const [map, setMap] = useState<google.maps.Map | null>(null);
8889

8990
useEffect(() => {
90-
new window.google.maps.Map(ref.current, {
91-
center,
92-
zoom,
93-
});
94-
});
91+
const map = new google.maps.Map(ref.current, {center, zoom});
92+
setMap(map);
93+
94+
return () => {
95+
google.maps.event.clearInstanceListeners(map);
96+
setMap(null);
97+
};
98+
}, []);
99+
100+
useEffect(() => {
101+
if (!map) return;
102+
103+
// do something with the map instance
104+
}, [map]);
95105

96106
return <div ref={ref} id="map" />;
97107
}

0 commit comments

Comments
 (0)