visgl / visgl/react-map-gl

[Bug] Layer component must have source prop when not an immediate descendent of Source component

Open
#2,099 2 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
TypeScript
Stars
8.5k
Forks
1.4k
Avg merge
5d 17h
Merged PRs (30d)
3

Description

Description

Documentation for Layer states the following.

source is required by some layer types in the Mapbox style specification. If is used as the child of a Source component, this prop will be overwritten by the id of the parent source.

This documentation implies to me that specifying a source for a Layer is optional in some situations, and that the source is meaningless if nested under a Source, because then the prop is overwritten by the id of the parent source.

That does seem to be the case in my testing when a Layer is a direct/immediate child/descendant of a Source.

For reasons unclear to me it seems that if a Layer component is NOT a direct child, like if it's wrapped in MyHelperWhateverComponent, then the automatic source <=> layer connection breaks. In this case I must pass an explicit source for the Layer to work properly with the Source data despite what the documentation says.

<Source
  id="the-source"
  type="geojson"
  data={{
    type: "FeatureCollection",
    features: [
      {
        type: "Feature",
        properties: {
          lat_y: 37.8,
          long_x: -122.4,
          nation1: null,
          nation2: null,
          note: null
        },
        geometry: {
          type: "Point",
          coordinates: [-122.4, 37.8]
        }
      }
    ]
  }}
>
  {/* This wraps the exact same functionality as the Layer below. It is seemingly identical to the code below, but requires an explicit sourceId or it will not render the Layer within */}
  <MyLayer sourceId="the-source" />

  {/* No source prop required. Source is inferred automatically and things work perfectly fine */
  {/* <Layer
    type="symbol"
    layout={{
      "icon-allow-overlap": true,
      "icon-ignore-placement": true,
      "text-allow-overlap": true,
      "text-ignore-placement": true,
      "icon-size": 1,
      "icon-image": "myImage"
    }}
  /> */}
</Source>
Expected Behavior

Based on the documentation I would assume that a Layer does not need a source specified if it's a child of a Source. Regardless of the exact tree hierarchy of components.

Steps to Reproduce

https://codesandbox.io/s/cranky-night-ni263i?file=/src/index.js

  • Create a <Source /> layer without an explicit id
  • Create an immediate descendent <Layer /> without an explicit source id
  • See the layer render using the source data
  • Create an abstraction, a component of some sort that contains the same <Layer /> as above
  • See <Layer /> does not render without an explicit source id attached
Environment
  • Framework version: "react-map-gl": "7.0.2" (default when using Code Sandbox boiler plate)
  • Map library: "mapbox-gl": "1.13.2" (default when using Code Sandbox boiler plate)
  • Browser: Chrome Latest 109.0.5414.87 (Official Build) (x86_64)
  • OS: macOS Latest 13.0.1 (22A400)
Logs

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked CodeSandbox reproduction in src/index.js and compare direct-child behavior with the wrapped MyLayer case. Then trace the Source/Layer component relationship described in the issue; done when source inference works through a wrapper and the reproduction no longer needs an explicit sourceId.

Written by the indexing model from the issue text.

Assessment

Tech stack
react, typescript
Domain
frontend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.