ubilabs / google-maps-react-hooks

The JavaScript library to easily implement a Google Maps map into your react application. It comes with a collection of React hooks to access the map instance or different Maps JavaScript Services.
MIT License
77 stars 13 forks source link

useGoogleMap() returns undefined #143

Closed ktranish closed 1 year ago

ktranish commented 1 year ago
import {
  GoogleMapsProvider,
  useGoogleMap,
} from "@ubilabs/google-maps-react-hooks";
import { useCallback, useEffect, useState } from "react";

export default function Example() {
  const map = useGoogleMap();
  const [mapContainer, setMapContainer] = useState<HTMLDivElement | null>(null);
  const mapRef = useCallback((node: HTMLDivElement) => {
    node && setMapContainer(node);
  }, []);
  const mapOptions = {
    center: { lat: 59.27240992362248, lng: 15.213029186736854 },
    zoom: 16,
  };
  useEffect(() => {
    console.log(map); //undefined
  }, [map]);
  return (
    <GoogleMapsProvider
      googleMapsAPIKey={process.env.NEXT_PUBLIC_GOOGLE_MAPS_API_KEY as string}
      mapContainer={mapContainer}
      mapOptions={mapOptions}
    >
      <div ref={mapRef} className="h-96 w-full rounded-lg"></div>
    </GoogleMapsProvider>
  );
}
ktranish commented 1 year ago

Simple hooks like this aren't even usable. I wouldn't recommend this library to anyone trying it out. Don't waste your time on this and start looking for alternatives - https://www.npmjs.com/package/@react-google-maps/api

yfr commented 1 year ago

@nytrek The hooks can only be used when the provider is initialized. And then only in the child components of said provider. So your example code is lacking the right setup. Please have a look at the example in the README on the repos' homepage, right where you started.

RTFM, thanks!

sultanularefin commented 9 months ago

I find this package underrated and working very well in one of my React project(TypeScript) with parcel bundler, but not working in a nextjs(javaScript, not typeScript) project, both having same react version:

"next": "^14.0.4",
"react": "^18.2.0",