React components for Leaflet maps.
In development, use at your own risks.
Tests and documentation still being worked on.
npm install react-leaflet
React and Leaflet are peer dependencies, if you haven't already installed them use:
npm install leaflet react react-leaflet
All components are React wrappers for Leaflet elements and layers, they need a map instance and therefore must be included in a top-level <Map>
component.
Leaflet example
import L from "leaflet";
const position = [51.505, -0.09];
const map = L.map("map").setView(position, 13);
L.tileLayer("http://{s}.tile.osm.org/{z}/{x}/{y}.png", {
attribution: '© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
L.marker(position).addTo(map)
.bindPopup("A pretty CSS3 popup. <br> Easily customizable.");
React-Leaflet
import React from "react";
import {Map, Marker, Popup, TileLayer} from "react-leaflet";
const position = [51.505, -0.09];
const map = <Map center={position} zoom={13}>
<TileLayer
url="http://{s}.tile.osm.org/{z}/{x}/{y}.png"
attribution='© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
/>
<Marker position={position}>
<Popup>
<span>A pretty CSS3 popup.<br/>Easily customizable.</span>
</Popup>
</Marker>
</Map>;
React.render(map, document.getElementById("map-container"));
Note that the <Map>
component creates its own <div>
container for the map, it does not get attached to an existing node.
- At this point, not all layers are implemented and even less tested.
- Properties on most components are static: they are set during the first render but not updated when the component updates. Check the documentation and source code to know what properties are dynamic.
LatLng: One of [Number, Number]
, {lat: Number, lng: Number}
or {lat: Number, lon: Number}
.
LatLngList: An Array of LatLng.
Bounds: An instance of Leaflet.LatLngBounds or a LatLngList.
Leaflet exposes its own events, different from React. You can listen to them using React-Leaflet by adding a callback to a property prefixed by onLeaflet
or simply on
. Ex: <Map onLeafletMoveend={this.handleMoveend}>...</Map>
.
Check Leaflet documentation for the events associated to each component.
The properties documented for each component are the ones aimed to be supported (tested and made dynamic when possible) by React-Leaflet.
All other properties are passed as the options
argument to their corresponding Leaflet element and should work fine for static maps, it is however unlikely that they would updated if you change them afterwards.
You can directly access the Leaflet element created by a component using the leafletElement
property on this component. This leaflet element is usually created in componentWillMount()
, except for the Map
component where it can only be created after the <div>
container is rendered.
These components are base classes used by other components. They can be extended to create custom components but should not be used directly.
Base class extending React.Component
and handling events binding and unbind.
It exposes a getLeafletElement()
method to access the Leaflet
object created for the component.
Base class extending MapComponent
using the provided map
prop to add its element and passing it down to its children.
Base class extending MapLayer
with a render()
method and handling a TitleLayer opacity
and zIndex
props.
Base class extending MapLayer
with a render()
method passing its leafletElement
to its children as the popupContainer
prop.
This is the top-level component that must be mounted for children ones to be rendered. Refer to Leaflet documentation for more information about the properties.
Properties
center
(optional LatLng, dynamic): Center of the map. This property is dynamic, if you change it it will be reflected in the map.id
(optional String): The ID of the<div>
container for the map. If you don't provide it, a unique one will be created.maxBounds
(optional Bounds)maxZoom
(optional Number)minZoom
(optional Number)zoom
(optional Number, dynamic)
position
(required LatLng, dynamic)
The Popup children will be rendered as its content using React.renderToStaticMarkup()
, they must be valid React elements.
position
(optional LatLng, dynamic)
url
(required String, dynamic)opacity
(optional Number, dynamic)zIndex
(optional Number, dynamic)
url
(required String, dynamic)opacity
(optional Number, dynamic)attribution
(optional String)
- CanvasTileLayer
- WMSTileLayer
center
(required LatLng, dynamic)radius
(required Number, dynamic)
center
(required LatLng, dynamic)radius
(optional Number, dynamic)
positions
(required LatLngList, dynamic)
polylines
(required Array of LatLngList, dynamic)
positions
(required LatLngList, dynamic)
polygons
(required Array of LatLngList, dynamic)
bounds
(required Bounds, dynamic)
- FeatureGroup
- GeoJson
- Removed
getLeafletElement()
deprecation. - Updated Babel to v5.
Released v0.4.
React 0.13.0.
- Updated React dependency to 0.13.0-rc2:
- Components are defined as ES6 classes.
- Mixins are replaced by the base components
MapComponent
,MapLayer
,BaseTileLayer
andPopupContainer
. All components extend from these. - The new
React.cloneElement()
API is used instead of the deprecatedReact.addons.cloneWithProps()
to pass themap
property to the components. - The
map
property has been removed from the componentspropTypes
definition as it is dynamically injected to its children by theMap
component, React would now warn it is not set. It is still required by components to have access to the Leaflet object.
- Events can now be set as
on{Event}
rather thanonLeaflet{Event}
, exonClick
instead ofonLeafletClick
, as all events are proxied to Leaflet. - Deprecated
getLeafletElement()
method, simply use theleafletElement
property instead to access the Leaflet object created for a component.
MIT
See LICENSE file.