React-leaflet 在模态框中显示不正确。

huangapple go评论72阅读模式
英文:

React-leaflet don't show correctly in Modal

问题

大家好!我尝试在我的React项目中使用react-leaflet,并采用Materialize样式。当我打开模态组件时,我看不到地图。我已经安装了npm install react-leaflet,还在index.html中添加了以下链接:

<link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css"
integrity="sha256-p4NxAoJBhIIN+hmNHrzRCf9tD/miZyoHS5obTRR9BMY="
crossorigin=""/>

请帮助我解决这个问题。

英文:

everyone! I try to use react-leaflet in my React Project with Materialize style. When i open Modal component i don't see map. I installed npm install react-leaflet, also added in index.html &lt;link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css"
integrity="sha256-p4NxAoJBhIIN+hmNHrzRCf9tD/miZyoHS5obTRR9BMY="
crossorigin=""/&gt;

React-leaflet 在模态框中显示不正确。

import React, { useEffect, useRef } from &quot;react&quot;;
import PropTypes from &quot;prop-types&quot;;
import { MapContainer, TileLayer, Marker, Popup } from &quot;react-leaflet&quot;;

export const MapModal = ({ lnglat }) =&gt; {
	const mapRef = useRef(null);

	useEffect(() =&gt; {
		// Clean up the map instance when the modal is closed
		return () =&gt; {
			if (mapRef.current) {
				mapRef.current.remove();
			}
		};
	}, []);

	return (
		&lt;div id=&#39;MapModal&#39; className=&#39;modal&#39;&gt;
			&lt;div className=&#39;modal-content&#39;&gt;
				&lt;MapContainer
					center={[48.0196, 66.9237]}
					zoom={5}
					style={{ height: &quot;50vh&quot;, width: &quot;100%&quot; }}
					ref={mapRef}
				&gt;
					&lt;TileLayer
						attribution=&#39;&amp;copy; &lt;a href=&quot;https://www.openstreetmap.org/copyright&quot;&gt;OpenStreetMap&lt;/a&gt; contributors&#39;
						url=&#39;https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png&#39;
					/&gt;
					&lt;Marker position={lnglat}&gt;
						&lt;Popup&gt;
							A pretty CSS3 popup. &lt;br /&gt; Easily customizable.
						&lt;/Popup&gt;
					&lt;/Marker&gt;
				&lt;/MapContainer&gt;
			&lt;/div&gt;
		&lt;/div&gt;
	);
};

MapModal.propTypes = {
	lnglat: PropTypes.array,
};

Help me please how to solve the problem?

答案1

得分: 1

Add this to imports import 'leaflet/dist/leaflet.css'; 我遇到了同样的问题,这个解决了它。

英文:

Add this to imports import &#39;leaflet/dist/leaflet.css&#39;; I had the same problem and this fixed it.

huangapple
  • 本文由 发表于 2023年6月8日 17:01:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76430231.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定