react-leaflet-markercluster: Typescript error: 'MarkerClusterGroup' cannot be used as a JSX component.
Hi, I’m trying to use it with TypeScript but I’m receiving the following error:
TypeScript error in /src/components/DashCard/Map/Map.tsx(71,12):
'MarkerClusterGroup' cannot be used as a JSX component.
Its instance type 'MarkerClusterGroup<any>' is not a valid JSX element.
Type 'MarkerClusterGroup<any>' is missing the following properties from type 'ElementClass': render, context, setState, forceUpdate, and 3 more. TS2786
69 | url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
70 | />
> 71 | <MarkerClusterGroup>
| ^
72 | <Marker position={[-22.8878, -43.2993]}>
73 | <Popup>F12</Popup>
74 | </Marker>
The following packages versions are being used:
"react": "^17.0.1",
"react-dom": "^17.0.1",
"leaflet": "^1.7.1",
"react-leaflet": "^3.0.4",
"react-leaflet-markercluster": "^3.0.0-rc1",
"typescript": "4.1.2",
Anyone has a workaround on this?
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 4
- Comments: 19
Commits related to this issue
- chore(package): Create additional type declaration for react-leaflet-clustermarker https://github.com/yuzhva/react-leaflet-markercluster/issues/133 — committed to GeotrekCE/Geotrek-rando-v3 by deleted user 6 years ago
As a workaround I’ve declared a .d.ts file with the following content:
If you need to pass props to MarkerClusterGroup, you’ll have to change this file too.
I think it should be
Hey all,
The reason for this issue is the fact that this library relies on DefinitelyTyped for its typing, but the DefinitelyTyped definition only provides typing for version 2.0, which relies on
react-leafletv2’s typing, which is completely different from how the new library works.The ideal solution would be to convert this library to TypeScript; this would mean type definitions would be provided natively, and would be based on the types for
react-leafletv3, which also provides its own native typing.Note that it appears another user has made their own library for marker clusters.
EDIT: For those reading this thread later, the library I linked now uses the MIT license and is thus safe to use for closed-source projects.
I was getting an error Non-abstract class ‘MarkerClusterGroup’ does not implement inherited abstract member ‘render’ from class ‘Component<MarkerClusterGroupOptions, {}>’. ts(2515) so I tweaked @DorianMazur code like this:
Thanks @piotr-cz And for newbies like me:
typeswith filereact-leaflet-markercluster.d.tsin the root of the projectreact-leaflet-markercluster@2.0.0installed This caused the page to not load. Deletenpm uninstall react-leaflet-markerclusterInstall latest versionnpm install react-leaflet-markerclusterRight now it isreact-leaflet-markercluster@3.0.0-rc1types/react-leaflet-markercluster.d.tsfile with contents abovetsconfig.jsonthanks a lot @Silventino it worked like a charm! I don’t know if someone is still looking to maintain this package and the types, but it could be a good PR to open
Again, thanks a lot!
Thanks @Silventino, the workaround worked for me