react-leaflet-markercluster: Importing MarkerClusterGroup results in TypeError

I am receiving the following error when trying to use this package.

TypeError: Super expression must either be null or a function, not object

The entire error is shown here: image

The error is associated with these lines of code in the react-leaflet-markcluster.js file (line numbers differ from what is shown in the error because of Chrome debugger formatting) image

I am trying to use the package in a Gatsby project, but I don’t think that is causing the error. I am successfully able to import and use the react-leaflet project.

Version Info:

"react-leaflet": "^2.0.0",
"react-leaflet-markercluster": "^1.1.8",
"leaflet": "^1.3.1",
"leaflet.markercluster": "^1.3.0",
"gatsby": "^1.9.273",

Related Import Statements

import {
  Map,
  TileLayer,
  Marker,
  Popup,
} from 'react-leaflet';
import 'leaflet/dist/leaflet.css';
import MarkerClusterGroup from 'react-leaflet-markercluster/dist/react-leaflet-markercluster';
import 'react-leaflet-markercluster/dist/styles.min.css'; 

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 2
  • Comments: 39 (10 by maintainers)

Most upvoted comments

@webcarrot Many thanks for your contribution!

@reggie3 @oserban @ThiefMaster

Currently, the latest version with support of react-leaflet v2.0 and React 16.3 context API available as candidate release:

yarn add react-leaflet-markercluster@next # yarn
npm install react-leaflet-markercluster@next # npm

After the README and demo-app will be updated will publish it as release package.

P.S: it’s stable and I think there would no be any changes in final release, so enjoy it (=

Temporary version:

import { MapLayer, withLeaflet } from "react-leaflet";
import L from "leaflet";

require("leaflet.markercluster");

class MarkerClusterGroup extends MapLayer {

  createLeafletElement(props) {
    const el = new L.markerClusterGroup(props);
    this.contextValue = {
      ...props.leaflet,
      layerContainer: el
    };
    return el;
  }

}

export default withLeaflet(MarkerClusterGroup);

For me, with react-leaflet 2 worked solution from #issuecomment-403071677 (WITHOUT USING THIS PACKAGE react-leaflet-markercluster)

import { MapLayer, withLeaflet } from '../src'
import L from "leaflet";

require("leaflet.markercluster");

class MarkerClusterGroup extends MapLayer {

  createLeafletElement(props) {
    const el = new L.markerClusterGroup(props);
    this.contextValue = {
      ...props.leaflet,
      layerContainer: el
    };
    return el;
  }

}

export default withLeaflet(MarkerClusterGroup);

Dependencies version:

    "react-leaflet": "2.2.1",
    "leaflet": "^1.4.0",
    "leaflet.markercluster": "^1.4.1",

@webdobe

You need to install all peerDependencies:

yarn add leaflet.markercluster leaflet react-leaflet prop-types

Then install next release of that plugin:

yarn add react-leaflet-markercluster@next

Previously try to remove node_modules and clean all lock files if you have them to reset prev. versions.

@AliBayatpour - adding:

declare module ‘react-leaflet-markercluster’ { import { Component } from ‘react’;

// eslint-disable-next-line react/prefer-stateless-function
export default class MarkerClusterGroup extends Component { }

}

to a global.d.ts file, fixed the “‘MarkerClusterGroup’ cannot be used as a JSX component” for me.

I was also getting type errors, e.g. “TypeError: Cannot read property ‘removeLayer’ of undefined”. However, using the code posted by @adambisek plus a css import, it seems to work. Here is the (slightly) modified code,

import { MapLayer, withLeaflet } from 'react-leaflet';
import L from "leaflet";

require("leaflet.markercluster");
require('react-leaflet-markercluster/dist/styles.min.css');

class MarkerClusterGroup extends MapLayer {

  createLeafletElement(props) {
    const el = new L.markerClusterGroup(props);
    this.contextValue = {
      ...props.leaflet,
      layerContainer: el
    };
    return el;
  }

}

export default withLeaflet(MarkerClusterGroup);

In case someone using Next.js stumbles upon here and use react-leaflet-universal (as I did), I confirm the rc3 works properly! You have to use next’s dynamic() to import it client-side though.

import { Marker } from 'react-leaflet-universal'
//https://github.com/zeit/next.js/wiki/FAQ
//https://stackoverflow.com/questions/52939439/dynamic-import-node-module-with-next-js
import dynamic from 'next/dynamic'
const MarkerClusterGroup = dynamic(import('react-leaflet-markercluster'), {ssr: false})

...

<Map 
  //Custom component wrapping react-leaflet's Map, also loaded with dynamic()
  //Don't forget to add the maxZoom, or it'll die
  maxZoom={10} 
  >
    <MarkerClusterGroup>
        <Marker … />
        <Marker … />
        <Marker … />
    </MarkerClusterGroup>
</Map>

Deps :

    "leaflet": "^1.5.1",
    "leaflet.markercluster": "^1.4.1",
    "react-leaflet": "^2.3.0",
    "react-leaflet-markercluster": "^2.0.0-rc3",
    "react-leaflet-universal": "^2.1.0",

Temporary version:

import { MapLayer, withLeaflet } from "react-leaflet";
import L from "leaflet";

require("leaflet.markercluster");

class MarkerClusterGroup extends MapLayer {

  createLeafletElement(props) {
    const el = new L.markerClusterGroup(props);
    this.contextValue = {
      ...props.leaflet,
      layerContainer: el
    };
    return el;
  }

}

export default withLeaflet(MarkerClusterGroup);

import { withLeaflet,Marker} from "react-leaflet"; Attempted import error: ‘withLeaflet’ is not exported from ‘react-leaflet’.

Hello, @Riuborth

Currently, this wrapper does not work with react-leaflet v3 - the issue is already created

I will take a look at how it could be implemented with v3 during those weekends.

any contribution is welcome

UPD: The latest version with support of react-leaflet v3.0 available as a candidate release:

yarn add react-leaflet-markercluster@next # yarn
npm install react-leaflet-markercluster@next # npm

Hi.

  1. In react-leaflet-markercluster@next version markers pop-ups do not display content - probably they need map from props.leaflet to be set/provided in MarkerClusterGroup instance contextValue.
  2. In my temporary version events handlers are “automagically” used by L.markerClusterGroup plugin so probably there is no need to reduce/filter/split props in createLeafletElement to handle events manually.
  3. This temporary version simply drop support for deprecated API (markers, options etc.).

Sorry for my English.

@ThiefMaster yep, I will try to deliver it during the next week. The trick is that react-leaflet was as the candidate release till that day.

So since today, I need to update this lib. I will update package.json dependency to solve that issue in v1.1.9.

And since v2.0 there will be support of react-leaflet ^2.0 latest React context from 16.3

Attempted import error: ‘withLeaflet’ is not exported from ‘react-leaflet’.

@asgaraliyev In case, if you are using v3 of react-leaflet - that example is outdated, as it has been written for v2.

@saadsaifse first result from googling “mapbox cluster” https://docs.mapbox.com/mapbox-gl-js/example/cluster/

@YUzhva

I’m still getting the above error and I’m using 2.0.0-rc3

package.json

{
    "leaflet": "^1.4.0",
    "leaflet.markercluster": "^1.4.1",
    "react-leaflet": "^2.2.0",
    "react-leaflet-markercluster": "^2.0.0-rc3",
}

component

import { Map, CircleMarker, Tooltip, TileLayer } from 'react-leaflet';
import MarkerClusterGroup from 'react-leaflet-markercluster';

component.less

@import '~leaflet/dist/leaflet.css?url=false';
@import '~leaflet.markercluster/dist/MarkerCluster.css';
@import '~leaflet.markercluster/dist/MarkerCluster.Default.css';
@import '~react-leaflet-markercluster/dist/styles.min.css';