我有一个React-Leaflet地图,我正在渲染div
里面。
出于某种原因,与 div 的内容交互会导致下面的地图做出响应(例如:双击将缩放地图,拖动将平移地图) - 即使我正在调用e.stopPropagation()
附加到 div 的处理程序。
据我了解,调用stopPropagation()
应该防止 DOM 事件到达地图本身。
为什么看起来stopPropagation()
被忽略了?
我怎样才能在地图内渲染一个 div而不让它的事件冒泡到地图本身?
import { Map, TileLayer } from 'react-leaflet';
const MyMap = props => (
<Map zoom={13} center={[51.505, -0.09]}>
<TileLayer url={"http://{s}.tile.osm.org/{z}/{x}/{y}.png"} />
{/*
HOW do I get this div to NOT pass it's events down to the map?!?!
Why does e.stopPropagation() appear to not work?
*/}
<div
id="zone"
onClick={e => e.stopPropagation()}
onMouseDown={e => e.stopPropagation()}
onMouseUp={e => e.stopPropagation()}
>
<p>Double-click or click and drag inside this square</p>
<p>Why does the map zoom/pan?</p>
</div>
</Map>
);
ReactDOM.render(<MyMap />, document.getElementById('root'));