Skip to content

Commit

Permalink
Fix stale onLoad and onError event handlers (#1505)
Browse files Browse the repository at this point in the history
  • Loading branch information
khmm12 authored Sep 6, 2021
1 parent f96184e commit 376dc5c
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/mapbox/mapbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,11 @@ export default class Mapbox {
});
};

_handleError = event => {
// @ts-ignore
this.props.onError(event);
};

_reuse(props) {
this._map = Mapbox.savedMap;
// When reusing the saved map, we need to reparent the map(canvas) and other child nodes
Expand Down Expand Up @@ -272,8 +277,8 @@ export default class Mapbox {
this._map = new this.mapboxgl.Map(Object.assign({}, mapOptions, props.mapOptions));

// Attach optional onLoad function
this._map.once('load', props.onLoad);
this._map.on('error', props.onError);
this._map.once('load', this._fireLoadEvent);
this._map.on('error', this._handleError);
}

return this;
Expand All @@ -288,8 +293,8 @@ export default class Mapbox {
Mapbox.savedMap = this._map;

// deregister the mapbox event listeners
this._map.off('load', this.props.onLoad);
this._map.off('error', this.props.onError);
this._map.off('load', this._fireLoadEvent);
this._map.off('error', this._handleError);
this._map.off('styledata', this._fireLoadEvent);
} else {
this._map.remove();
Expand Down

0 comments on commit 376dc5c

Please sign in to comment.