It seems that eventHandlers, although it is available as a prop on MapContainer (if you press ctlr + space on vscode f.i it will pop up) it is not available in the official API and is intended only for child components of MapContainer, see here and here for Marker.
what you want to achieve can be implemented using useMapEvents on a separate comp and then included as a child on MapContainer:
function App() {
function MyComponent() {
const map = useMapEvents({
click: (e) => {
const { lat, lng } = e.latlng;
L.marker([lat, lng], { icon }).addTo(map);
}
});
return null;
}
return (
<MapContainer center={[50.5, 30.5]} zoom={13} style={{ height: "100vh" }}>
<TileLayer
attribution='© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
/>
<MyComponent />
</MapContainer>
);
}
Demo
Another approach is listen to whenReady prop (not officially documented but seems to work similarly to whenCreated prop but map instance is accessible via object.target) on MapContainer:
<MapContainer
center={[50.5, 30.5]}
zoom={13}
style={{ height: "100vh" }}
whenReady={(map) => {
console.log(map);
map.target.on("click", function (e) {
const { lat, lng } = e.latlng;
L.marker([lat, lng], { icon }).addTo(map.target);
});
}}
>
...
</MapContainer>
3rd approach is to use MapConsumer as a child of MapContainer (docs):
<MapContainer center={[50.5, 30.5]} zoom={13}>
<MapConsumer>
{(map) => {
console.log("map center:", map.getCenter());
map.on("click", function (e) {
const { lat, lng } = e.latlng;
L.marker([lat, lng], { icon }).addTo(map);
});
return null;
}}
</MapConsumer>
</MapContainer>
4th approach is to use whenCreated prop (officially documented) on MapContainer:
<MapContainer
center={[50.5, 30.5]}
zoom={13}
style={{ height: "100vh" }}
whenCreated={(map) => {
map.on("click", function (e) {
const { lat, lng } = e.latlng;
L.marker([lat, lng], { icon }).addTo(map.target);
});
}}
>
...