Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

react-leaflet Components cannot be wrapped in React components #73

Closed
uniphil opened this issue Sep 23, 2015 · 14 comments
Closed

react-leaflet Components cannot be wrapped in React components #73

uniphil opened this issue Sep 23, 2015 · 14 comments

Comments

@uniphil
Copy link
Contributor

uniphil commented Sep 23, 2015

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
@uniphil
Copy link
Contributor Author

uniphil commented Sep 23, 2015

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 uniphil closed this as completed Sep 23, 2015
@Smallmelo
Copy link

@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?

@jgimbel
Copy link
Contributor

jgimbel commented Jun 16, 2016

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>
    );
  }
}

@Smallmelo
Copy link

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

@taozhi8833998
Copy link

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

@Nazmulho71
Copy link

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

@cbrizuelaromero
Copy link

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';

@leilabaneshi
Copy link

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

@helinagirmay
Copy link

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

@cbrizuelaromero
Copy link

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

which version do you use?

@ashutoshm1771
Copy link

Use 'MapContainer' instead of 'Map'

@MichelMuemboIlunga
Copy link

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:\Users\Private\node_modules@react-leaflet\core\esm\index.js: Unexpected character '' (1:0)

1 |
| ^
at parser.next ()

@TimMcCauley
Copy link

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

@nirmalyathakurta
Copy link

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:\Users\Private\node_modules@react-leaflet\core\esm\index.js: Unexpected character '' (1:0)

1 |
| ^
at parser.next ()

Having the same issue after updating map to mapcontainer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests