My example with dataToShow = $state() wasn't perfect, but I didn't exactly know how to convey the idea that the GeoMap component's updates exist outside of the Svelte reactivity system and therefore are not automatically reflected in the UI.
Gotcha...I suppose you don't get any event when geoMap source changes right? Are you setting this? If you are you could use a separate stateful variable that you use to trigger the effect
As a matter of fact I just realized there’s indeed a callback ‘SourceAddedCallback’ that I could use to react to source initialization.
Btw it’s mapbox-gl I’m working with. I’m not super familiar with it yet
Comments
$effect(() => {
const source = geoMap.getSource('source-id')
if (source?.type === 'geojson') {
source.setData(...)
}
}
I think you issue is showData = $state()... i think this should be derived or linked derived/ mutable derived for showData
If I do this:
geoMap = new GeoMap();
geoMap.addSource(…);
sourceAdded = true;
And then use sourceAdded in the effect, it works exactly as expected! No need for the snapshot anymore
Btw it’s mapbox-gl I’m working with. I’m not super familiar with it yet
https://svelte.dev/playground/6fd552d6918e43fdbd513b064fe49a0b?version=5.19.3