This guide explains how to work with layers in a CARTO for React application.
If you use the code generator for adding your layers (see below), it will create a
CartoLayer with default rendering and interactivity properties.
If you are working with the (CartoLayer), you need to associate it with a data source. The source provides
connection, and, optionally,
credentials properties that are used by CARTO for deck.gl to retrieve the vector tiles.
If you want to sync the layer with one or more widgets, you can only use CARTO for deck.gl layers because filters are applied through the source.
Sources are usually created with the code generator. If you use
stores as the name for your source, the code generator will create a new file named
storesSource.js in the
src/data folder with the following content:
Creating a layer
You can create layers manually but it is easier to use the code generator:
When you execute this command, you will need to select the name and source for your layer and you need to decide if you want to attach the layer to a view, so the layer is displayed in the map when the view is selected and is removed when the user switches to another view.
It creates a new file that exports a function with the name of the layer. This function returns a CARTO for deck.gl layer with default styling properties. The function retrieves the associated source from the store and use the source
If we introduce
Stores as the name in the code generator, a file named
StoresLayer.js will be created in the
src/components/layers folder with the following content:
The code generator will include the new layer in the
getLayers function within the
src/components/layers/index.js file. This function is used by the
<Map/> component to initialize the
If you decide to attach the layer to an existing view, the following code is added to the view:
dispatch function is used to dispatch an action to the Redux store. This is how this works:
The view dispatches the new source to the store.
The view dispatches the new layer to the store.
The Map Component is re-rendered since the store has changed.
The Map Component get all the layers in the store and draw them.
This is one of the benefits of reactive programming: we can add the layer from any place in the application just by dispatching the right action.
If you use the code generator, the code will create a
CartoLayer with default styling properties. To learn more about customizing the style properties for your layers, please read the Customizing the CartoLayer style guide in the CARTO for deck.gl documentation.
The first parameter passed to the layer constructor when using the code generator is
...cartoLayerProps. This is a set of default properties that are used mainly for setting the source properties, and filtering and highlighting features. You can get more details about the
useCartoLayerProps hook in the library reference.
If you need to override the
uniqueIdProperty, used in vector tile layers to identify features, you need to specify the property name when you call the hook. This is useful for filtering and highlighting when a feature crosses or is present in multiple tiles:
If you need to override any of the properties configured by the hook, you must include them in the layer constructor after
...cartoLayerProps. For instance, if you want to add your own
updateTriggers property, you could add the following code to maintain the current behavior and add your own update trigger for a given accessor (
useCartoLayerProps hook also defines handlers for
CartoLayer events like
onViewportLoad. If you need to add additional functionality to these handlers, you can listen to the events but you must call the original handler to ensure the app keeps working as expected:
Support for other deck.gl layers
If you want to use a different deck.gl layer (i.e. ArcLayer, TripsLayer), read this section in the deck.gl documentation to learn more.
This is the summary:
To create a layer you need to:
- Define a function that returns a deck.gl layer.
- Export the layer ID as a constant.
The layer must be added to the map component layers array.
You need to add the source and the layer to the store when the view is initialized.