react-leaflet-markercluster: TypeError: Cannot read property 'lat' of undefined
I’m using react-leaflet-markercluster with with children markers and getting TypeError: Cannot read property 'lat' of undefined when I update the markers. From the readme it looks like this is not fully supported yet. Could you expand on this?
I think the cause might be how keys are applied to the cloned marker element.
From my understanding React uses the key to determine on update if the item is new or an updated earlier one and the react docs advise against using index as a key.
https://facebook.github.io/react/docs/lists-and-keys.html
Keys help React identify which items have changed, are added, or are removed. Keys should be given to the elements inside the array to give the elements a stable identity:
When you don’t have stable IDs for rendered items, you may use the item index as a key as a last resort
I’m trying to debug this but having issues building the project. At first glance I thought it was the node version. I went from v8 to v4 but still not building. Could you let me know which node version the project targets. Might also be worth adding the engines property in the package.json with the targeted node. I can also create a seperate issue for this specifically if you think it might be best.
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Reactions: 3
- Comments: 22 (4 by maintainers)
Happy you found it helpful!
Yeah I totally get that, it’s just the nature of open source work, right? 😜
So after digging into this for a few hours yesterday, here’s a summary of what I found…
Actual Error
@whyvez I’m not sure what the stack trace was for you, however the core of it for me was:
So, after digging into various fixes within this library for a while with no luck, I decided to come back to the highlighted lines. Here’s what I found:
These lines within
CircleMarkerare the “source” of the error. Seeing that thecenterprop is either an array, object, orLatLnginstance – the equality check doesn’t actually work (though I’m guessing it’s dependent on object identity or something). Those two arrays were actually equal in my case and when the check is replaced with alatLng(...).equalscall…The
setLatLngcode is never executed, thus resolving the error. The thing I still haven’t been able to figure out is why thesetLatLngcall fails in the first place. ThetoProps.centervalue is defined when it’s called so something else must be going on behind the scenes. Maybe we can ping theleafletandreact-leafletmaintainers to get a better idea.Thoughts on this Package
So, while the bug may not be related to this package, and aside from what I mentioned above re CSS and build process, there were some other things I noticed about the base component.
First, I don’t think you should support
markersandmarkersOptionsprops. They increase the complexity of your component significantly and aren’t available as options in the baseleaflet.markerclusterpackage as far as I can tell. Rather, I think you should just support markers as children as it’s fairly painless for users to consume it this way, e.g. instead ofmarkers={ points }they could just write:This would be a breaking change obviously, but I think it’s a worthwhile one. Which leads me to my second point. With
markersandmarkersOptionsremoved, I was able to simplify this component a great deal:This offsets most of the work on child elements calling
addLayeron the marker container and avoids cloning or any react element generation which feels a bit funny imho. Also if you rethink the CSS stuff and offset that to consumers as well the wholecomponentWillMountmethod goes away.Anyway, I just figured I’d share what I’d found. I’m still fairly new to
react-leafletbut it seems this direction would be closer to what’s recommended in their guide. As mentioned above, I’d be happy to push my changes to a fork or branch (if given permissions). I don’t have enough bandwidth to take this all the way on my own atm, but I’d be happy to advise someone who does. I maintain the new webpack documentation so I’d definitely be able able to help guide the build process refactoring if desired.For now, I’m just going to create a custom
CircleMarkerinstance with that fix.@skipjack Your solution is awesome. I was “playing” with it during those weekends and there everything was awesome!
So, I decided to implement it and publish to npm with test coverage… (you know, all those cool labels at repository)
But there were issues… I supposed to finish with your solution and test coverage during the next weekends. Hope to publish it (=
Hi, maybe you should to review the dependency tree, I had the same problem, from a library loads leaflet from its node_modules folder, also, my app was loading leaflet from its node_modules folder, Leaflet did a instanceOf comparison. Some as:
https://github.com/Leaflet/Leaflet/blob/master/src/geo/LatLngBounds.js#L246
For some reason,
ais a instance of LatLngBounds loaded from my app node_modules folder leaflet loaded andbis null, buttoLatLngBoundsis from a library node_modules folder, of course,a instanceof LatLngBoundsreturns false, its going toreturn new LatLngBounds(a, b);line, and returns aLatLngBoundsempty.I was finally able to fix the error, setting the leaflet load on config-overrides.js
@elesdoar : thanks, you save my day 🎉
Getting the same issues with the cluster children, “Uncaught TypeError: Cannot read property ‘0’ of undefined”, after updating a cluster.
@whyvez I was able to clone and build though I did have to make a few tweaks. I’m on Node v6.9.4 and NPM v3.10.10. Let me know if you’re still interested in this as I could share a fork.
@YUzhva there are probably a few things that could be simplified in relation to config and deployment:
requirebaked in (related to #40). This means all consumers must be using webpack and have acss-loaderconfigured. Instead users can just decide whether or not to include the CSS as they see fit.Anyway, just thought I’d mention the things I noticed while going through this as some clean up might make the package easier to maintain/contribute to.