istarkov / google-map-clustering-example

clustering example for google-map-react
http://istarkov.github.io/google-map-clustering-example/
200 stars 76 forks source link

help with props #5

Closed cezarneaga closed 7 years ago

cezarneaga commented 7 years ago

Hi there,

in the clustering example you are using data from a static file. i don't have experience with recompose unfortunately and i got stuck.

i have a use case where i want to load the GMap.js inside App.js. App holds the state and the markersData would be in the state. how can i load this into GMap.js as props and then if state changes clusters change with it? thanks Cezar

istarkov commented 7 years ago

Example will work even with dynamic data, just get the state not from withState https://github.com/istarkov/google-map-clustering-example/blob/master/src/GMap.js#L56-L60 but from redux (connect) or any other source

istarkov commented 7 years ago

To be clear - everywhere in the source I assume that markers can be changed, like https://github.com/istarkov/google-map-clustering-example/blob/master/src/GMap.js#L89-L91

cezarneaga commented 7 years ago

i do not use redux on this. i guess this is my question: how do i get state into GMap.js from App.js? do I need withState instances:

withState(
    'markers',
    'setMarkers',
    markersData
  ),
  withState(
    'hoveredMarkerId',
    'setHoveredMarkerId',
    -1
  ),
  withState(
    'mapProps',
    'setMapProps',
    {
      center: { lat: 46.2, lng: 24 },
      zoom: 7,
    }
  ),

should they be withProps? please if you have time, give me a quick example Thanks, C

istarkov commented 7 years ago

Have you tried just to start write your code? It's 10x faster just to try pass markers as a property and remove or not withState to check what you will get vs writing a question. Also recompose have a good documentation about it's enhancers ;-) It's simple code, just start writing and all will be fine ;-)

cezarneaga commented 7 years ago

:) @istarkov, i tried. i wouldnt have written otherwise. i used react-google-maps and i displayed clusters there but that package doesnt handle redrawing of markers on state changes. this is how i got to your package. only mentioning this to say that i started... :)

C

cezarneaga commented 7 years ago

please explain just how i could get the markers( from props.markers) into the compose function

istarkov commented 7 years ago

Just remove withState('markers' and pass markers as a property to GMap https://github.com/istarkov/google-map-clustering-example/blob/master/src/Layout.js#L23 like <GMap markers={yourMarkers}.

Be sure that all such properties u use in immutable manner, so if any marker has changed, reference to array of markers must also be changed.

Otherwise withPropsOnChange with cluster creating logic will not be called.

cezarneaga commented 7 years ago

that i already did. i can see them in React Dev Tools. i get this

warning.js:36 Warning: flattenChildren(...): Encountered two children with the same key, `.$1_242`. Child keys must be unique; when two children share a key, only the first child will be used.
    in div (created by GoogleMapMarkers)
istarkov commented 7 years ago

It seems that you have markers with duplicate id's. As current source uses id's as key for react elements in array https://github.com/istarkov/google-map-clustering-example/blob/master/src/GMap.js#L33-L34

React doc's about keys https://facebook.github.io/react/docs/reconciliation.html#keys

cezarneaga commented 7 years ago

checked that. its not. id's are unique generated with math.random eg: "50d84f0e1e601"

i think id: ${numPoints}_${points[0].id} might be the culprit

istarkov commented 7 years ago

It might not, as points in every cluster are different, so taking an id of a first point in each cluster will give unique id's for clusters too

cezarneaga commented 7 years ago

thanks @istarkov. will come back to this another day and keep trying.