Sitemap
Abdibnu

Abdibnu

React Leaflet

2 min readDec 27, 2022

React leaflet is react package that binds between react and leaflet. It does not replace Leaflet, but leverages it to abstract Leaflet layers as React components. React Leaflet uses React’s context API to make some Leaflet elements instances available to children elements that need it.

Each Leaflet map instance has its own React context, created by the MapContainer component. Other components and hooks provided by React Leaflet can only be used as descendants of a MapContainer.

Get Abdibnu’s stories in your inbox

Join Medium for free to get updates from this writer.

Life cycle process of leaflet can be explained :

  1. The MapContainer renders a container <div> element for the map. If the placeholder prop is set, it will be rendered inside the container <div>.
  2. The MapContainer instantiates a Leaflet Map for the created <div> with the component properties and creates the React context containing the map instance.
  3. The MapContainer renders its children components.
  4. Each child component instantiates the matching Leaflet instance for the element using the component properties and context, and adds it to the map.
  5. When a child component is rendered again, changes to its supported mutable props are applied to the map.
  6. When a component is removed from the render tree, it removes its layer from the map as needed.

But react leaflet have some limitations such as :

  • Leaflet makes direct calls to the DOM when it is loaded, therefore React Leaflet is not compatible with server-side rendering.
  • The components exposed are abstractions for Leaflet layers, not DOM elements. Some of them have properties that can be updated directly by calling the setters exposed by Leaflet while others should be completely replaced, by setting an unique value on their key property so they are properly handled by React's algorithm.

The use of this packages to our project is this package support leaflet from openstreetmap map for input in our project. beside that using this package u can also manipulate such as creating points, pop up points, route creation and many more

--

--