Skip to content
Advertisement

React-Leaflet Map doesn’t update

My issue is that react-leaflet <MapContainer> doesn’t center on a position that I set dynamically. The basic logic is that I have a form where I enter Street and House Number, then I make call for Nominatim and get some JSON format data, from where I extract latitude and longitude of a building. These lat and long I pass to my <MapContainer> but it doesn’t respond anyhow. With react-leaflet v2 with it was working pretty good, but after I updated to v3 it stopped.

Whenever I set default position values MapContainer centers on that position. But when I pass another value dynamically through Nominatim call it doesn’t work.

Here I make call for Nominatim:

JavaScript

This is onSubmit of my form:

JavaScript

And here is my MapBox component which contains my leaflet Map:

JavaScript

Advertisement

Answer

I was able to solve it. In documentation it is stated as:

Except for its children, MapContainer props are immutable: changing them after they have been set a first time will have no effect on the Map instance or its container. The Leaflet Map instance created by the MapContainer element can be accessed by child components using one of the provided hooks or the MapConsumer component.

This piece of code helps to understand:

JavaScript

What I implemented:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement