import React from 'react'; import mapboxgl from 'mapbox-gl'; import MapContext from '../MapContext'; /** * MapView backed by Mapbox GL KS */ class MapView extends React.Component< { styleURL: string; children: JSX.Element }, { map?: object | null } > { state = { map: null }; mapContainer: HTMLElement | null = null; map: object | null = null; componentDidMount() { const { styleURL } = this.props; if (!this.mapContainer) { console.error('MapView - mapContainer should is null'); return; } const map = new mapboxgl.Map({ container: this.mapContainer, style: styleURL || 'mapbox://styles/mapbox/streets-v11', }); this.map = map; this.setState({ map }); } render() { const { children } = this.props; const { map } = this.state; return (
(this.mapContainer = el)} > {map && (
{children}
)}
); } } export default MapView;