More robust add/remove condition checks (#1840)

This commit is contained in:
Xiaoji Chen 2022-04-21 11:58:22 -07:00 committed by GitHub
parent 1c391281a1
commit 2e49a545c8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 3 additions and 2 deletions

View File

@ -29,6 +29,7 @@ export default function App() {
bearing: 80,
pitch: 80
}}
maxPitch={85}
mapStyle="mapbox://styles/mapbox/satellite-v9"
mapboxAccessToken={TOKEN}
terrain={{source: 'mapbox-dem', exaggeration: 1.5}}

View File

@ -61,7 +61,7 @@ function updateLayer(map: MapboxMap, id: string, props: LayerProps, prevProps: L
function createLayer(map: MapboxMap, id: string, props: LayerProps) {
// @ts-ignore
if (map.style && map.style._loaded && map.getSource(props.source)) {
if (map.style && map.style._loaded && (!('source' in props) || map.getSource(props.source))) {
const options: LayerProps = {...props, id};
delete options.beforeId;
@ -90,7 +90,7 @@ function Layer(props: LayerProps) {
return () => {
map.off('styledata', forceUpdate);
// @ts-ignore
if (map.style && map.style._loaded) {
if (map.style && map.style._loaded && map.getLayer(id)) {
map.removeLayer(id);
}
};