Skip to content

Instantly share code, notes, and snippets.

@andrerocker
Last active January 27, 2023 19:34
Show Gist options
  • Save andrerocker/939e66def8bbee0f7a87b1d619fffddb to your computer and use it in GitHub Desktop.
Save andrerocker/939e66def8bbee0f7a87b1d619fffddb to your computer and use it in GitHub Desktop.
react + google-maps: @googlemaps/react-wrapper (Yes It's really need at least two components)
import { Wrapper } from "@googlemaps/react-wrapper";
const ParentComponentThatWillReceiveGoogleMap = () => {
return <Wrapper apiKey={"42-l337-lol-bbq"}>
<MyGoogleMapComponent/>
</Wrapper>
}
const MyGoogleMapComponent = () => {
const ref = useRef(null);
const [map, setMap] = useState();
useEffect(() => {
if (ref.current && !map) {
setMap(new window.google.maps.Map(ref.current, {
center: {lat: -23.9851934, lng: -46.2332309},
zoom: 16
});
}
}, [map]);
return <div style={{height: "800px", width: "100%"}} ref={ref}></div>
}
@andrerocker
Copy link
Author

andrerocker commented Jan 27, 2023

It's not production-ready (It's not releasing map-instance properly / useEffect)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment