Map component: Cannot read property 'initialize' of undefined
Asked Answered
B

2

8

I'm trying to get a basic map working using react-leaflet.js. Here's my code:

import React from 'react';
import { Map } from 'leaflet';

const mapStyle = {
  height: '400px'
};

const mapCenter = [0, -0];
const zoomLevel = 3;

export default class MapT extends React.PureComponent {
  render() {
    return (
      <div>
        <Map
          style={mapStyle}
          center={mapCenter}
          zoom={zoomLevel}
        >
        </Map>
      </div>
    );
  }
}

But I'm getting an error:

Uncaught TypeError: Cannot read property 'initialize' of undefined
    at NewClass (leaflet-src.js:300)
    at mountIndeterminateComponent (react-dom.development.js:14003)
    at beginWork (react-dom.development.js:14470)
    at performUnitOfWork (react-dom.development.js:17014)
    at workLoop (react-dom.development.js:17054)
    at HTMLUnknownElement.callCallback (react-dom.development.js:149)
    at Object.invokeGuardedCallbackDev (react-dom.development.js:199)
    at invokeGuardedCallback (react-dom.development.js:256)
    at replayUnitOfWork (react-dom.development.js:16366)
    at renderRoot (react-dom.development.js:17148)
    at performWorkOnRoot (react-dom.development.js:17993)
    at performWork (react-dom.development.js:17901)
    at performSyncWork (react-dom.development.js:17873)
    at requestWork (react-dom.development.js:17761)
    at scheduleWork (react-dom.development.js:17566)
    at Object.enqueueSetState (react-dom.development.js:11695)

What am I doing wrong?

Bobbinet answered 22/11, 2018 at 14:7 Comment(0)
B
22

You need to import the React leaflet components from react-leaflet instead of leaflet.

import { Map } from 'react-leaflet';
Backfire answered 22/11, 2018 at 14:27 Comment(1)
Never thought it would be a problem if I just auto import in VS Code, saved my day!Cavalry
F
0

Sorry to be so late. In above example there is not value for width. If someone have the same issue, make sure to have: height and width setted on map component. For example:

If it doesn't works, change value for "position" in style

Freshen answered 10/11, 2019 at 9:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.