React-leaflet: How to set the AttributionControl's prefix?

Created on 19 Nov 2015  路  8Comments  路  Source: PaulLeCam/react-leaflet

In raw Leaflet, this would look like:

this.map.attributionControl.setPrefix("");

(It hides the "Leaflet |" prefix when displaying attribution.)

Most helpful comment

I figured it out!

Declare attributionControl={false} in the parent <ReactLeafletMap> component to disable to default attribution, then create a new <AttributionControl> component with prefix={false}, like so:

render() {
  const position = [this.state.lat, this.state.lng];
  return (
    <ReactLeafletMap
      center={position}
      zoom={this.state.zoom}
      zoomControl={false}
      attributionControl={false}
    >
      <TileLayer
        attribution="[attribution text]"
        url="[tile url]"
      />
      <AttributionControl position="bottomright" prefix={false} />
    </ReactLeafletMap>
  );
}

The actual attribution text, minus the prefix, is still declared in the attribution property of the <TileLayer> component.

All 8 comments

For straight-up attribution changes, passing options as props works great. However, for the attribution prefix, it's not enough to set options.attributionControl.prefix: "" (which would need to be passed as <Map ... {...{"attributionControl.prefix": ""}} />, if I'm understanding things right). (attributionControl is a boolean and is a bit too much of a hammer.)

No you can't set nested options like this in the properties, but as stated in the documentation, you can access Leaflet's map instance using getLeafletElement() on the Map component.

How exactly does this work? The context on the top level element is empty, so how could I access the map instance on Map level?

I'm struggling with this. My <TileLayer> element has an attribution value, but I want to hide the default "Leaflet" prefix. I can create an <AttributionControl> element with prefix={false}, but that just creates a duplicate attribution.

Example:

render() {
  const position = [this.state.lat, this.state.lng];
  return (
    <ReactLeafletMap
      center={position}
      zoom={this.state.zoom}
      zoomControl={false}
    >
      <TileLayer
        attribution="[attribution text]"
        url="[tile url]"
      />
      <AttributionControl position="bottomright" prefix={false} />
    </ReactLeafletMap>
  );
}

Screenshot (two attributions, one without the "Leaflet" prefix):

screen shot 2018-02-05 at 1 35 44 pm

@mojodna, I am curious, were you able to find a good solution for removing the prefix via these React components?

@MattSidor no, i end up embedding Leaflet directly in my own components instead since I'm not doing anything complicated (or if I am, it's a weird fit with this plugin).

I figured it out!

Declare attributionControl={false} in the parent <ReactLeafletMap> component to disable to default attribution, then create a new <AttributionControl> component with prefix={false}, like so:

render() {
  const position = [this.state.lat, this.state.lng];
  return (
    <ReactLeafletMap
      center={position}
      zoom={this.state.zoom}
      zoomControl={false}
      attributionControl={false}
    >
      <TileLayer
        attribution="[attribution text]"
        url="[tile url]"
      />
      <AttributionControl position="bottomright" prefix={false} />
    </ReactLeafletMap>
  );
}

The actual attribution text, minus the prefix, is still declared in the attribution property of the <TileLayer> component.

Should be

<AttributionControl position="bottomright" prefix=""} />

otherwise

Failed prop type: Invalid prop prefix of type boolean supplied to AttributionControl, expected string.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

thenickcox picture thenickcox  路  4Comments

Shadowman4205 picture Shadowman4205  路  4Comments

robinmetral picture robinmetral  路  4Comments

treydavis picture treydavis  路  4Comments

mrafei picture mrafei  路  4Comments