We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 5f26c70 commit 0f9d68bCopy full SHA for 0f9d68b
1 file changed
README.md
@@ -84,14 +84,24 @@ function MyMapComponent({
84
center: google.maps.LatLngLiteral;
85
zoom: number;
86
}) {
87
- const ref = useRef();
+ const ref = useRef<HTMLDivElement>();
88
+ const [map, setMap] = useState<google.maps.Map | null>(null);
89
90
useEffect(() => {
- new window.google.maps.Map(ref.current, {
91
- center,
92
- zoom,
93
- });
94
+ const map = new google.maps.Map(ref.current, {center, zoom});
+ setMap(map);
+
+ 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]);
105
106
return <div ref={ref} id="map" />;
107
}
0 commit comments