I have a React-Leaflet map which I am rendering a div
inside.
For some reason, interacting with the contents of the div causes the map beneath to respond (eg: double-clicking will zoom the map, dragging will pan the map) - even when I'm calling e.stopPropagation()
in handlers attached to the div.
As I understand it, calling stopPropagation()
should prevent the DOM events from ever reaching the map itself.
Why does it appear that stopPropagation()
is being ignored?
How can I render a div inside the map without it's events bubbling to the map itself?
Here is an example codepen showing the problem.
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'));
e.stopPropagation()
on the div itself doesn't work tho. How does Leaflet receive these events if they're not passed up from the div? – Real