react-leaflet Components cannot be wrapped in React components

Created on 23 Sep 2015  路  13Comments  路  Source: PaulLeCam/react-leaflet

In apps I work on, we often want to do things like:

  • Have different views over the same map (maybe controlled by react-router)
  • Share customized Marker wrapper code between different views.

For both of these use-cases we should be able to make new React components that can be placed on the map, containing only react-leaflet children.

Example code:

import React, { Component } from 'react';
import { Map, Marker } from 'react-leaflet';

class WrappedMarker extends Component {
  render() {
    return <Marker position={[51.505, -0.09]} />;
  }
}

export default class MapView extends Component {
  render() {
    return (
      <Map center={[51.505, -0.09]} zoom={13}>
        <WrappedMarker />
      </Map>
    );
  }
}

The above does not show the marker, and logs a warning:

Uncaught TypeError: Cannot read property 'addLayer' of undefined

Most helpful comment

Attempted import error: 'Map' is not exported from 'react-leaflet' (imported as 'LeafletMap').

I changed 'Map' to 'MapContainer' in the import:

_import { MapContainer as LeaftletMap, TileLayer } from 'react-leaflet';_

All 13 comments

Since React.Children.map only does a shallow mapping, this cannot be handled automatically.

The wrapping component can pass this.props.map down to its child manually and it works.

@uniphil i'm running into this same problem, and if you closed this ticket because you found a solution, it is unclear to me what it was. can you elaborate?

Here is an example of how to create custom components, and the documentation.

TLDR; Notice line 5. When creating a custom component, the map and layerContainer props must be passed down.

The initial example can be fixed by adding these props to the Marker.

import React, { Component } from 'react';
import { Map, Marker } from 'react-leaflet';

class WrappedMarker extends Component {
  render() {
    const { map, layerContainer } = this.props; //Given by the `Map` component
    return (
      <Marker 
        map={map} /* pass down to Marker */
        layerContainer={layerContainer} /* pass down to Marker */
        position={[51.505, -0.09]} 
      />
    );
  }
}

export default class MapView extends Component {
  render() {
    return (
      <Map center={[51.505, -0.09]} zoom={13}>
        <WrappedMarker />
      </Map>
    );
  }
}

thank you, @jgimbel. that cleared things up. makes much more sense.

in react-leaflet 1.1.1version, the map and layerContainer is not in props of child component

Attempted import error: 'Map' is not exported from 'react-leaflet' (imported as 'LeafletMap').

Attempted import error: 'Map' is not exported from 'react-leaflet' (imported as 'LeafletMap').

I changed 'Map' to 'MapContainer' in the import:

_import { MapContainer as LeaftletMap, TileLayer } from 'react-leaflet';_

Attempted import error: 'Map' is not exported from 'react-leaflet' (imported as 'LeafletMap').

I changed 'Map' to 'MapContainer' in the import:

_import { MapContainer as LeaftletMap, TileLayer } from 'react-leaflet';_

thanks it works for me

Attempted import error: 'MapContainer' is not exported from 'react-leaflet'....getting this error

Attempted import error: 'MapContainer' is not exported from 'react-leaflet'....getting this error

which version do you use?

Use 'MapContainer' instead of 'Map'

After changed from 'Map' to 'MapContainer' in the import I got this error

C:/Users/Private/node_modules/@react-leaflet/core/esm/index.js
SyntaxError: C:[email protected]: Unexpected character '' (1:0)

1 |
| ^
at parser.next ()

I had a similar issue and came up with this solution https://codesandbox.io/s/react-leaflet-overlay-import-wu6ct?file=/src/index.js:333-704

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ekatzenstein picture ekatzenstein  路  4Comments

thenickcox picture thenickcox  路  4Comments

kojoa picture kojoa  路  3Comments

rolfdalhaug picture rolfdalhaug  路  3Comments

kaitlynbrown picture kaitlynbrown  路  3Comments