Leaflet: Map container not found Leaflet: Map container not found reactjs reactjs

Leaflet: Map container not found


The <div id="leafletmap"> must be added to the dom before calling L.map('leafletmap').


In addition to @IvanSanchez's response, You could add the geolocation and L.map(...) code to componentDidMount() React lifecycle method (depending on what other goals you hope to achieve). You could also create and bind event handlers for location found as well.

This way must have been added the dom and leaflet can find it.

Happy to help with this if it's still unclear.


I hope this helps if you are using Angular:

const container = document.getElementById('map')if(container) {    // code to render map here...}