MCPcopy Create free account
hub / github.com/visgl/react-map-gl / reuse

Method reuse

modules/react-mapbox/src/mapbox/mapbox.ts:243–285  ·  view source on GitHub ↗
(props: MapboxProps, container: HTMLDivElement)

Source from the content-addressed store, hash-verified

241 }
242
243 static reuse(props: MapboxProps, container: HTMLDivElement): Mapbox {
244 const that = Mapbox.savedMaps.pop();
245 if (!that) {
246 return null;
247 }
248
249 const map = that.map;
250 // When reusing the saved map, we need to reparent the map(canvas) and other child nodes
251 // intoto the new container from the props.
252 // Step 1: reparenting child nodes from old container to new container
253 const oldContainer = map.getContainer();
254 container.className = oldContainer.className;
255 while (oldContainer.childNodes.length > 0) {
256 container.appendChild(oldContainer.childNodes[0]);
257 }
258 // Step 2: replace the internal container with new container from the react component
259 // @ts-ignore
260 map._container = container;
261
262 // Step 4: apply new props
263 that.setProps({...props, styleDiffing: false});
264 map.resize();
265 const {initialViewState} = props;
266 if (initialViewState) {
267 if (initialViewState.bounds) {
268 map.fitBounds(initialViewState.bounds, {...initialViewState.fitBoundsOptions, duration: 0});
269 } else {
270 that._updateViewState(initialViewState, false);
271 }
272 }
273
274 // Simulate load event
275 if (map.isStyleLoaded()) {
276 map.fire('load');
277 } else {
278 map.once('styledata', () => map.fire('load'));
279 }
280
281 // Force reload
282 // @ts-ignore
283 map._update();
284 return that;
285 }
286
287 /* eslint-disable complexity,max-statements */
288 _initialize(container: HTMLDivElement) {

Callers 1

_MapFunction · 0.45

Calls 2

setPropsMethod · 0.45
_updateViewStateMethod · 0.45

Tested by

no test coverage detected