From 8fb083bb4db3902334e46430408561a868511e5a Mon Sep 17 00:00:00 2001 From: Martin Schuhfuss Date: Tue, 11 Feb 2025 11:39:27 +0100 Subject: [PATCH] docs: update example in readme. --- README.md | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 912ffbeb..d715f03a 100644 --- a/README.md +++ b/README.md @@ -84,14 +84,24 @@ function MyMapComponent({ center: google.maps.LatLngLiteral; zoom: number; }) { - const ref = useRef(); + const ref = useRef(); + const [map, setMap] = useState(null); useEffect(() => { - new window.google.maps.Map(ref.current, { - center, - zoom, - }); - }); + const map = new google.maps.Map(ref.current, {center, zoom}); + setMap(map); + + return () => { + google.maps.event.clearInstanceListeners(map); + setMap(null); + }; + }, []); + + useEffect(() => { + if (!map) return; + + // do something with the map instance + }, [map]); return
; }