Child components
Child components can only be used as descendants of a MapContainer component.
Props
Child components in React Leaflet use their props as options when creating the corresponding Leaflet instance, as described in Leaflet's documentation.
By default these props should be treated as immutable, only the props explicitely documented as mutable in this page will affect the Leaflet element when changed.
Behaviors
Child components in React Leaflet support common behaviors described below, implementing logic related to React or Leaflet.
Referenceable behavior
Makes the Leaflet instance for the given component accessible with the useRef()
hook.
function MyComponent() {
const circleRef = useRef()
useEffect(() => {
const radius = circleRef.current.getRadius()
})
return <Circle ref={cicleRef} center={[50.5, 30.5]} radius={200} />
}
ParentComponent behavior
The component will render its mutable React children components, based on the children?: ReactNode
prop.
<Marker position={[50.5, 30.5]}>
<Popup>Hello world</Popup>
</Marker>
Evented behavior
Adds support for the eventHandlers?: LeafletEventHandlerFnMap
prop, adding and removing event listeners.
<Marker
position={[50.5, 30.5]}
eventHandlers={{
click: () => {
console.log('marker clicked')
},
}}
/>
Attribution behavior
Applies to layer components, making their attribution
prop mutable.
<GeoJSON attribution="© credits due..." data={...} />
Pane behavior
Applies to layer components, adding support for the pane
prop or context from a Pane
component ancestor.
<Circle center={[50.5, 30.5]} radius={200} pane="my-existing-pane" />
<Pane name="custom" style={{ zIndex: 100 }}>
<Circle center={[50.5, 30.5]} radius={200} />
</Pane>
Path behavior
Applies to vector layer components, adding support for a pathOptions: PathOptions
mutable prop.
<Circle center={[50.5, 30.5]} radius={200} pathOptions={{ color: 'blue' }} />
MediaOverlay behavior
Applies to components using Leaflet's ImageOverlay class, adding support for mutable bounds: LatLngBounds
, opacity: number
and zIndex: number
props.
const bounds = new LatLngBounds([40.712216, -74.22655], [40.773941, -74.12544])
<ImageOverlay
url="http://www.lib.utexas.edu/maps/historical/newark_nj_1922.jpg"
bounds={bounds}
opacity={0.5}
zIndex={10}
/>
CircleMarker behavior
Applies to components using Leaflet's CircleMarker class, adding support for mutable center: LatLngExpression
and radius: number
props.
<Circle center={[50.5, 30.5]} radius={200} />
GridLayer behavior
Applies to components using Leaflet's GridLayer class, adding support for mutable opacity: number
and zIndex: number
props.
<TileLayer url="..." opacity={0.5} zIndex={10} />
Control behavior
Applies to control components, making their position: ControlPosition
prop mutable.
<ZoomControl position="bottomleft" />
UI layers
Marker
Props
Prop | Type | Required | Mutable | Behavior |
---|---|---|---|---|
attribution | string | No | Yes | Attribution |
children | ReactNode | No | Yes | ParentComponent |
draggable | boolean | No | Yes | |
eventHandlers | LeafletEventHandlerFnMap | No | Yes | Evented |
icon | Leaflet.Icon | No | Yes | |
opacity | number | No | Yes | |
pane | string | No | No | Pane |
position | LatLngExpression | Yes | Yes | |
ref | RefObject<Leaflet.Marker> | No | Yes | Referenceable |
zIndexOffset | number | No | Yes |
Popup
Props
Prop | Type | Required | Mutable | Behavior |
---|---|---|---|---|
attribution | string | No | Yes | Attribution |
children | ReactNode | No | Yes | ParentComponent |
eventHandlers | LeafletEventHandlerFnMap | No | Yes | Evented |
onClose | () => void | No | Yes | |
onOpen | () => void | No | Yes | |
pane | string | No | No | Pane |
position | LatLngExpression | No | Yes | |
ref | RefObject<Leaflet.Popup> | No | Yes | Referenceable |
Tooltip
Props
Prop | Type | Required | Mutable | Behavior |
---|---|---|---|---|
attribution | string | No | Yes | Attribution |
children | ReactNode | No | Yes | ParentComponent |
eventHandlers | LeafletEventHandlerFnMap | No | Yes | Evented |
onClose | () => void | No | Yes | |
onOpen | () => void | No | Yes | |
pane | string | No | No | Pane |
position | LatLngExpression | No | Yes | |
ref | RefObject<Leaflet.Tooltip> | No | Yes | Referenceable |
Raster layers
TileLayer
Props
Prop | Type | Required | Mutable | Behavior |
---|---|---|---|---|
eventHandlers | LeafletEventHandlerFnMap | No | Yes | Evented |
opacity | number | No | Yes | GridLayer |
pane | string | No | No | Pane |
ref | RefObject<Leaflet.TileLayer> | No | Yes | Referenceable |
url | string | Yes | No | |
zIndex | number | No | Yes | GridLayer |
WMSTileLayer
Props
Prop | Type | Required | Mutable | Behavior |
---|---|---|---|---|
eventHandlers | LeafletEventHandlerFnMap | No | Yes | Evented |
opacity | number | No | Yes | GridLayer |
pane | string | No | No | Pane |
params | WMSParams | No | Yes | |
ref | RefObject<Leaflet.TileLayer.WMS> | No | Yes | Referenceable |
url | string | Yes | No | |
zIndex | number | No | Yes | GridLayer |
ImageOverlay
Props
Prop | Type | Required | Mutable | Behavior |
---|---|---|---|---|
attribution | string | No | Yes | Attribution |
bounds | LatLngBounds | Yes | Yes | MediaOverlay |
children | ReactNode | No | Yes | ParentComponent |
eventHandlers | LeafletEventHandlerFnMap | No | Yes | Evented |
opacity | number | No | Yes | MediaOverlay |
ref | RefObject<Leaflet.ImageOverlay> | No | Yes | Referenceable |
url | string | Yes | Yes | |
zIndex | number | No | Yes | MediaOverlay |
VideoOverlay
Props
Prop | Type | Required | Mutable | Behavior |
---|---|---|---|---|
attribution | string | No | Yes | Attribution |
bounds | LatLngBounds | Yes | Yes | MediaOverlay |
eventHandlers | LeafletEventHandlerFnMap | No | Yes | Evented |
play | boolean | No | Yes | |
ref | RefObject<Leaflet.VideoOverlay> | No | Yes | Referenceable |
url | string , string[] or HTMLVideoElement | Yes | Yes | |
zIndex | number | No | Yes | MediaOverlay |
Vector layers
Circle
Props
Prop | Type | Required | Mutable | Behavior |
---|---|---|---|---|
attribution | string | No | Yes | Attribution |
center | LatLngExpression | Yes | Yes | |
children | ReactNode | No | Yes | ParentComponent |
eventHandlers | LeafletEventHandlerFnMap | No | Yes | Evented |
pane | string | No | No | Pane |
pathOptions | PathOptions | No | Yes | Path |
radius | number | Yes | Yes | |
ref | RefObject<Leaflet.Circle> | No | Yes | Referenceable |
CircleMarker
Props
Prop | Type | Required | Mutable | Behavior |
---|---|---|---|---|
attribution | string | No | Yes | Attribution |
center | LatLngExpression | Yes | Yes | |
children | ReactNode | No | Yes | ParentComponent |
eventHandlers | LeafletEventHandlerFnMap | No | Yes | Evented |
pane | string | No | No | Pane |
pathOptions | PathOptions | No | Yes | Path |
radius | number | Yes | Yes | |
ref | RefObject<Leaflet.CircleMarker> | No | Yes | Referenceable |
Polyline
Props
Prop | Type | Required | Mutable | Behavior |
---|---|---|---|---|
attribution | string | No | Yes | Attribution |
children | ReactNode | No | Yes | ParentComponent |
eventHandlers | LeafletEventHandlerFnMap | No | Yes | Evented |
pane | string | No | No | Pane |
pathOptions | PathOptions | No | Yes | Path |
positions | LatLngExpression[] or LatLngExpression[][] | Yes | Yes | |
ref | RefObject<Leaflet.Polyline> | No | Yes | Referenceable |
Polygon
Prop | Type | Required | Mutable | Behavior |
---|---|---|---|---|
attribution | string | No | Yes | Attribution |
children | ReactNode | No | Yes | ParentComponent |
eventHandlers | LeafletEventHandlerFnMap | No | Yes | Evented |
pane | string | No | No | Pane |
pathOptions | PathOptions | No | Yes | Path |
positions | LatLngExpression[] , LatLngExpression[][] or LatLngExpression[][][] | Yes | Yes | |
ref | RefObject<Leaflet.Polygon> | No | Yes | Referenceable |
Rectangle
Props
Prop | Type | Required | Mutable | Behavior |
---|---|---|---|---|
attribution | string | No | Yes | Attribution |
bounds | LatLngBoundsExpression | Yes | Yes | |
children | ReactNode | No | Yes | ParentComponent |
eventHandlers | LeafletEventHandlerFnMap | No | Yes | Evented |
pane | string | No | No | Pane |
pathOptions | PathOptions | No | Yes | Path |
ref | RefObject<Leaflet.Rectangle> | No | Yes | Referenceable |
SVGOverlay
Props
The attributes
must be valid SVGSVGElement
properties.
Prop | Type | Required | Mutable | Behavior |
---|---|---|---|---|
attributes | Record<string, string> | No | No | |
attribution | string | No | Yes | Attribution |
children | ReactNode | No | Yes | ParentComponent |
pane | string | No | No | Pane |
ref | RefObject<Leaflet.SVGOverlay> | No | Yes | Referenceable |
Other layers
LayerGroup
Props
Prop | Type | Required | Mutable | Behavior |
---|---|---|---|---|
attribution | string | No | Yes | Attribution |
children | ReactNode | No | Yes | ParentComponent |
eventHandlers | LeafletEventHandlerFnMap | No | Yes | Evented |
pane | string | No | No | Pane |
ref | RefObject<Leaflet.LayerGroup> | No | Yes | Referenceable |
FeatureGroup
Props
Prop | Type | Required | Mutable | Behavior |
---|---|---|---|---|
attribution | string | No | Yes | Attribution |
children | ReactNode | No | Yes | ParentComponent |
eventHandlers | LeafletEventHandlerFnMap | No | Yes | Evented |
pane | string | No | No | Pane |
ref | RefObject<Leaflet.FeatureGroup> | No | Yes | Referenceable |
GeoJSON
Props
Prop | Type | Required | Mutable | Behavior |
---|---|---|---|---|
attribution | string | No | Yes | Attribution |
children | ReactNode | No | Yes | ParentComponent |
data | GeoJsonObject | Yes | No | |
eventHandlers | LeafletEventHandlerFnMap | No | Yes | Evented |
pane | string | No | No | Pane |
ref | RefObject<Leaflet.GeoJSON> | No | Yes | Referenceable |
style | PathOptions or StyleFunction | No | Yes |
Controls
ZoomControl
Props
Prop | Type | Required | Mutable | Behavior |
---|---|---|---|---|
eventHandlers | LeafletEventHandlerFnMap | No | Yes | Evented |
position | ControlPosition | No | Yes | |
ref | RefObject<Leaflet.Control.Zoom> | No | Yes | Referenceable |
AttributionControl
Props
Prop | Type | Required | Mutable | Behavior |
---|---|---|---|---|
eventHandlers | LeafletEventHandlerFnMap | No | Yes | Evented |
position | ControlPosition | No | Yes | |
ref | RefObject<Leaflet.Control.Attribution> | No | Yes | Referenceable |
LayersControl
Props
Prop | Type | Required | Mutable | Behavior |
---|---|---|---|---|
collapsed | boolean | No | Yes | |
eventHandlers | LeafletEventHandlerFnMap | No | Yes | Evented |
position | ControlPosition | No | Yes | |
ref | RefObject<Leaflet.Control.Layers> | No | Yes | Referenceable |
LayersControl.BaseLayer
Props
Prop | Type | Required | Mutable | Behavior |
---|---|---|---|---|
checked | boolean | No | Yes | |
children | ReactNode | No | Yes | ParentComponent |
name | string | Yes | No |
LayersControl.Overlay
Props
Prop | Type | Required | Mutable | Behavior |
---|---|---|---|---|
checked | boolean | No | Yes | |
children | ReactNode | No | Yes | ParentComponent |
name | string | Yes | No |
ScaleControl
Props
Prop | Type | Required | Mutable | Behavior |
---|---|---|---|---|
eventHandlers | LeafletEventHandlerFnMap | No | Yes | Evented |
position | ControlPosition | No | Yes | |
ref | RefObject<Leaflet.Control.Scale | No | Yes | Referenceable |
Other
Pane
Props
The name
prop must be unique to the pane and different from the default Leaflet pane names
Prop | Type | Required | Mutable | Behavior |
---|---|---|---|---|
children | ReactNode | No | Yes | ParentComponent |
className | string | No | No | |
name | string | Yes | No | |
pane | string | No | No | Pane |
style | CSSProperties | No | No |