Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Map#getLayer returns undefined for the custom layer type #13239

Closed
moong23 opened this issue Jul 25, 2024 · 5 comments
Closed

Map#getLayer returns undefined for the custom layer type #13239

moong23 opened this issue Jul 25, 2024 · 5 comments
Labels

Comments

@moong23
Copy link

moong23 commented Jul 25, 2024

mapbox-gl-js version: 3.5.2

browser: chrome

Steps to Trigger Behavior

  • Only happens on v3.5.2 (tested on v.3.5.1 & under)
  1. With using MapboxOverlay, activate interleaved mode.
  2. With an interleaved mode, inifinite re-rendering happens

Link to Demonstration

https://codesandbox.io/p/sandbox/jovial-wing-f8y2xl?file=%2Fsrc%2FCustomMap.tsx%3A14%2C1
(think you need to log in to codesandbox to see this demo. Let me know if you want me to make a github repo-ed demo)

Expected Behavior

No re-rendering existing layer on state changes

Actual Behavior

when viewState changes, the layer tries to mount again.

@stepankuzmin
Copy link
Contributor

@moong23, could you please make a standalone reproduction of this? I.e., using only GL JS?

@moong23
Copy link
Author

moong23 commented Jul 26, 2024

@stepankuzmin well haven't tried to use GL JS only, but i'll try my best 😃

@Pessimistress
Copy link

deck.gl uses map.getLayer(id) to determine if a layer already exists in the stack. Starting 3.5.2, this method is returning undefined for a layer that is already added.

Here is a minimal reproduction:

const map = new mapboxgl.Map({
  container: 'map',
  style: 'mapbox://styles/mapbox/light-v9',
  accessToken: MAPBOX_TOKEN
});

const layer = {
  id: 'my-layer',
  type: 'custom',

  onAdd() {
    console.log(`Layer ${this.id} is added`);
  },

  onRemove() {
    console.log(`Layer ${this.id} is removed`);
  },

  render() {}
}

map.on('styledata', () => {
  if (map.getLayer(layer.id)) {
    // already added
  } else {
    map.addLayer(layer);
  }
});

Log:

Layer my-layer is added
evented.ts:172
Error: Layer with id "my-layer" already exists on this map
    at Da.addLayer (style.ts:2118:38)
    at Map.addLayer (map.ts:2813:20)
    at Map.<anonymous> (app.js:38:9)
    at Map.fire (evented.ts:150:26)
    at Map.<anonymous> (map.ts:768:18)
    at Map.fire (evented.ts:150:26)
    at Da.fire (evented.ts:166:24)
    at style.ts:1202:18
    at h2 (load_sprite.ts:55:13)
    at load_sprite.ts:37:27

@moong23
Copy link
Author

moong23 commented Jul 27, 2024

thx @Pessimistress

@stepankuzmin
Copy link
Contributor

Thanks, @Pessimistress. This is a bug.

@stepankuzmin stepankuzmin changed the title [Bug]: Infinite re-rendering happens with interleaved MapboxOverlay Map#getLayer returns undefined for the custom layer type Aug 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants