import React, { useEffect, useMemo, useRef, useState } from "react"; import mapboxgl from "mapbox-gl"; import { useTheme } from "next-themes"; import Image from "next/image"; const MapComponent: React.FC = () => { const { theme } = useTheme(); const mapContainerRef = useRef(null); const [mapLoaded, setMapLoaded] = useState(false); const style = useMemo(() => { return theme === "dark" ? "mapbox://styles/mapbox/dark-v10" : "mapbox://styles/mapbox/standard"; }, [theme]); useEffect(() => { const map = new mapboxgl.Map({ accessToken: process.env.NEXT_PUBLIC_MAPBOX_TOKEN, container: mapContainerRef.current as HTMLElement, style: style, center: [-118.4438, 34.0699], zoom: 10, }); map.on("load", () => { setMapLoaded(true); }); return () => map.remove(); // Cleanup on unmount }, [style]); return (
{!mapLoaded && ( Map )}
Icon
); }; export default MapComponent;