Source
This component allows apps to create a map source using React. It may contain Layer components as children.
import * as React from 'react';
import {Map, Source, Layer} from '@vis.gl/react-maplibre';
import type {CircleLayer} from '@vis.gl/react-maplibre';
import type {FeatureCollection} from 'geojson';
const geojson: FeatureCollection = {
type: 'FeatureCollection',
features: [
{type: 'Feature', geometry: {type: 'Point', coordinates: [-122.4, 37.8]}}
]
};
const layerStyle: CircleLayer = {
id: 'point',
type: 'circle',
paint: {
'circle-radius': 10,
'circle-color': '#007cbf'
}
};
function App() {
return <Map
initialViewState={{
longitude: -122.4,
latitude: 37.8,
zoom: 14
}}
mapStyle="https://demotiles.maplibre.org/style.json"
>
<Source id="my-data" type="geojson" data={geojson}>
<Layer {...layerStyle} />
</Source>
</Map>;
}
Properties
The props provided to this component should be conforming to the Mapbox source specification or CanvasSourceSpecification.
When props change shallowly, the component will attempt to update the source. Do not define objects/arrays inline to avoid perf hit.
Once a <Source>
is mounted, the following props should not change. If add/remove multiple JSX sources dynamically, make sure you use React's key prop to give each element a stable identity.
id
: string
Unique identifier of the source. If not provided, a default id will be assigned.
type
: string
Required. Type of the source.