Layer Updates and Dynamic Data

Update deck.gl layers in MapboxOverlay and understand stable IDs, data, accessors, and updateTriggers.

🎯 Introduction

By the end of this article, you will be able to update deck.gl data, styles, and filtered results without recreating the Mapbox map.

🧱 Why layers are usually recreated

A deck.gl layer configuration is declarative: you describe what should be drawn now, and deck.gl compares the old and new configurations to reuse GPU resources where possible. When data changes, you usually create a new layer instance rather than mutate fields on the old one.

function updateLayers(data) {
    const layer = new ScatterplotLayer({
        id: 'stations',
        data,
        getPosition: (item) => [item.lng, item.lat],
        getRadius: (item) => item.value,
    });

    overlay.setProps({
        layers: [layer],
    });
}

This updates the deck.gl overlay's layer list. The Mapbox map, basemap, and camera do not need to be recreated.

🔁 Updating data and accessors

If an accessor depends only on the current data item, replacing data and recreating the layer is usually enough:

const layer = new ScatterplotLayer({
    id: 'events',
    data: nextData,
    getPosition: (item) => item.position,
    getRadius: (item) => item.level * 8,
});

overlay.setProps({ layers: [layer] });

If an accessor also depends on state outside data, use updateTriggers to tell deck.gl which external value changes require it to run again:

const selectedType = 'priority';

const layer = new ScatterplotLayer({
    id: 'events',
    data,
    getPosition: (item) => item.position,
    getFillColor: (item) => item.type === selectedType
        ? [220, 38, 38, 220]
        : [37, 99, 235, 180],
    updateTriggers: {
        getFillColor: [selectedType],
    },
});

The array in updateTriggers.getFillColor lists the external dependencies of getFillColor. When selectedType changes, deck.gl recalculates the color attribute. Without this dependency, colors may retain their previous values.

⚡ When GPU attributes are recalculated

Common cases fall into three categories:

  • When data changes, accessors that depend on its records need to be recalculated.
  • When external state used by an accessor changes, flag it with updateTriggers.
  • When a layer's id, type, or key configuration changes, deck.gl reorganizes the corresponding resources.

This example uses MapboxOverlay for filtering and style updates without recreating the Mapbox basemap:

🪤 Stable IDs and lifecycle

Vue's ref, computed, and watch organize state. After state changes, generate a new layer array and call overlay.setProps({ layers }). Destroy the Mapbox map and overlay when the component unmounts to release event listeners and WebGL resources.

🧾 Summary

  • Layer updates are declarative: usually create a new configuration instead of modifying an old instance.
  • A stable id lets deck.gl match layers across updates.
  • Replacing data is usually enough when accessors depend only on data records.
  • Use updateTriggers when accessors depend on external state.
  • Updating deck.gl layers does not require recreating the Mapbox map.

❓ Questions and answers

Q1: Must I destroy MapboxOverlay on every data update?

No. Keep the overlay and update layers through overlay.setProps.

Q2: What does updateTriggers update?

It makes the specified accessor recalculate its GPU attributes, such as color, radius, or opacity.

Q3: Do I still need updateTriggers when data changes?

Usually not if the accessor reads only the current data item. If it also reads external state, declare those external dependencies.

🧪 Exercises

  1. Change selectedType in the example to 'regular' and observe the colors.
  2. Add updateTriggers.getRadius so that the radius depends on an external scale factor.

🎉 You can now update deck.gl data layers while keeping the map in place.