Make sure you have all peers dependencies required installed on your project.
React Version
React Mapbox GL Version
16.3.x
2.x.x
16.2.x
1.x.x
Example:
import React from 'react';
import { Map } from '@commodityvectors/react-mapbox-gl';
export default class MyMap extends React.Component {
render() {
return (
<Map {...this.props} accessToken={"YOUR_ACCESS_TOKEN"} mapStyle={'mapbox://styles/mapbox/streets-v9'}>
{this.props.children}
</Map>
);
}
}
Creating Custom Map Components
It is pretty simple to write your own map components. This library takes advantage of the new React 16.3 context API so you
can enrich you component with Map.component() method like below. This helper will automatically add a MapContext.Provider and inject the map as a prop.
Example:
import React from 'react';
import mapboxgl from 'mapbox-gl';
import { Map } from '@commodityvectors/react-mapbox-gl';
class FullscreenControl extends React.Component {
componentDidMount() {
const { map } = this.props;
this.control = new mapboxgl.FullscreenControl();
map.addControl(this.control);
}
componentWillUnmount() {
const { map } = this.props;
if(!map.isRemoving()) map.removeControl(this.control);
}
render() {
return null; // As this component won't change through render methods
}
}
export default Map.component(FullscreenControl);
Extending existing components
In order to extend an existing component, you have to propagate the props (or at least the map prop) to the child component
as the example below shows