Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invariant Violation: You need to provide libraries={["places"]} prop to <LoadScript /> component undefined #3418

Open
karan-wiz opened this issue Mar 21, 2025 · 1 comment

Comments

@karan-wiz
Copy link

"use client";
import React, { useState, useCallback, useEffect, useRef } from "react";
import {
GoogleMap,
useLoadScript,
Marker,
Autocomplete,
} from "@react-google-maps/api";

const LIBRARIES = ["places"];

const isMobile =
typeof window !== "undefined" &&
window.innerWidth > 250 &&
window.innerWidth < 600;

const mapContainerStyle = {
width: isMobile ? "100%" : "50%", // 640px corresponds to 'sm' size in Tailwind
height: "400px",
};

const defaultCenter = {
lat: 40.712776,
lng: -74.005974,
};

const GoogleMapComponent = ({
paddress,
setPaddress,
latlong,
setLatlong,
serachError,
onLocationSearch,
}) => {
const { isLoaded, loadError } = useLoadScript({
googleMapsApiKey: process.env.NEXT_PUBLIC_MAPS_KEY,
libraries: LIBRARIES,
});

const [marker, setMarker] = useState(null);
const [localAddress, setLocalAddress] = useState(paddress || "");

useEffect(() => {
if (latlong.latitude && latlong.longitude) {
setMarker({
lat: Number(latlong.latitude),
lng: Number(latlong.longitude),
});
}
}, [latlong]);

const [autocomplete, setAutocomplete] = useState(null);
const mapRef = useRef();

@karan-wiz
Copy link
Author

I have followed the code as per the document but when the map is loaded for the first time then the page breaks and when I load it for the second time then everything works fine

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant