[英]Why I Can't use Leaflet Map on Nextjs even with Dynamic Import?
Leaflet need global window object and as you know on SSR there is no window
and it will return an error window is not definded
Leaflet need global window object and as you know on SSR there is no
window
and it will return an error window is not definded
I searched a lot and the only way to use Leaflet it was to use nextjs dynamic
import but i get nothing In my Page我搜索了很多,使用 Leaflet 的唯一方法是使用 nextjs
dynamic
导入,但在我的页面中我什么也没得到
It's been a day I'm searching the web how to solve this and NOTHING worked so far for me.这一天我正在搜索 web 如何解决这个问题,到目前为止对我来说没有任何效果。
I Really need an answer for this我真的需要一个答案
Why Map component won't Render?为什么 Map 组件不会渲染?
Map.tsx Map.tsx
import { useState } from "react";
import { MapContainer, TileLayer, Marker, useMapEvents } from "react-leaflet";
import "leaflet/dist/leaflet.css";
import "leaflet-defaulticon-compatibility/dist/leaflet-defaulticon-compatibility.css";
import "leaflet-defaulticon-compatibility";
const Map = () => {
const [position, setPosition] = useState<[number, number]>([51.505, 51.505]);
// Add Marker on Map onClick
function Mark() {
const map = useMapEvents({
click: ({ latlng }) => {
setPosition([latlng.lat, latlng.lng]);
},
});
return <Marker position={position} />;
}
return (
<MapContainer
center={[35.7219, 51.3347]}
zoom={6}
scrollWheelZoom={false}
style={{ height: "50vh" }}
>
<TileLayer url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png" />
<Mark />
</MapContainer>
);
};
export default Map;
my dynamic import我的动态导入
import dynamic from "next/dynamic";
const AdPage = ({ ad }: AdPageProps): JSX.Element => {
const MapWithNoSSR = dynamic(() => import("./NewAd/map"), {
ssr: false,
});
return (
<>
<MapWithNoSSR />
</>
);
};
export default AdPage;
try to move MapWithNoSSR
out of AdPage
function尝试将
MapWithNoSSR
移出AdPage
function
import dynamic from "next/dynamic";
const MapWithNoSSR = dynamic(() => import("./NewAd/map"), {
ssr: false,
});
const AdPage = ({ ad }: AdPageProps): JSX.Element => {
return (
<>
<MapWithNoSSR />
</>
);
};
export default AdPage;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.