I have been struggling with an issue which comes down to me not being able to properly reference the map object. After researching it for a few days, I've come to the conclusion that the "old" (up to v3.x) whenCreated prop of the MapContainer, which returned the Leaflet.Map object, has been removed.
Now all that's left is the whenReady prop, which is defined as () => void
. Therefore I am struggling to access the map object, which I desperately need.
Does anyone have any tips for this?
The release notes for React Leaflet v4 state:
What you can do is define a state to store a reference to the map:
then when you create your
MapContainer
get a reference to the map:then define a
useEffect
where you can access themap
object:There's a StackBlitz here to demonstrate this. If you click anywhere on the map, the code above will execute and set the zoom level to 8.