helix

hadils 2024-03-31T22:30:28.213529Z

Hi! I have the following expression:

(map-indexed (fn [i coordinates]
                         ($ Polygon {:coordinates (clj->js coordinates)
                                        :strokeWidth 2
                                        :strokeColor "green"
                                        :fillColor "rgba(0,128,0,0.5)"}))
                       polygons)
I can’t get rid of the Warning: Each child in a list should have a unique "key" prop.

lilactown 2024-04-02T17:25:40.510899Z

components themselves do not have to handle :key. React handles the key prop specially

lilactown 2024-04-02T17:27:22.763679Z

the code you pasted looks correct on a skim, so I'm not sure why it would cause a "unique key" warning. perhaps it's in some other part of the code?

lilactown 2024-04-02T17:28:20.886999Z

above you mentioned ^{:key (str i)} - this only works in Reagent when using the vector syntax. This attaches metadata to the vector which reagent then uses to attach the key to the underlying React element. it won't work in helix using $

hadils 2024-04-02T17:28:52.638229Z

Thank you @lilactown. I am stumped as to why I am getting this error.

lilactown 2024-04-02T17:29:06.902169Z

are you sure it's pointing at this specific code?

hadils 2024-04-02T17:29:47.231119Z

I will double-check, but the error refers to MapPolygons specifically, I have ns’ed it as Polygon

lilactown 2024-04-02T17:31:13.756849Z

I would also investigate other potential issues, like problems with code reloading and build caching

hadils 2024-04-02T20:37:59.617909Z

Solved. I did not require the polygon component correctly.

rafaeldelboni 2024-04-01T15:50:03.919869Z

In the map with the props add an :key i

hadils 2024-04-01T15:51:21.177169Z

The props for Polygon ? I tried that, without success.

rafaeldelboni 2024-04-01T15:53:03.025569Z

Do you have another component doing the same map-indexed and using i as key? If so try appending the component name in the key like :key (str "polygon" i)

hadils 2024-04-01T16:01:11.926729Z

I do not have another component like that. I have tried: • Putting ^{:key (str i)} in front of the ($ Polygon …) • Adding a :key (str i) to the props

rafaeldelboni 2024-04-01T16:05:31.291739Z

Maybe wrap Polygon with a div and add the key on the wrapping div

rafaeldelboni 2024-04-01T16:06:13.940969Z

For me usually just adding the key in the components works for me

hadils 2024-04-01T16:06:21.305719Z

I can try that…Thanks @rafaeldelboni

rayat 2024-04-01T17:40:08.889849Z

I’ve always fixed this warning by adding the key prop to the component like so:

(map-indexed (fn [i coordinates]
                         ($ Polygon {:key i
                                        :coordinates (clj->js coordinates)
                                        :strokeWidth 2
                                        :strokeColor "green"
                                        :fillColor "rgba(0,128,0,0.5)"}))
                       polygons)
The times that wouldn’t work is, as stated above, if the i is not unique, in which case using some other guaranteed unique identifier is needed. Curious if the div trick doesn’t work

hadils 2024-04-01T17:43:17.253019Z

Thanks @rayatrahman9. I need to explain more fully: This is React Native, not React. I cannot put this particular component in a div because it is a child of MapView. Here’s the full component:

(defnc YardView
  [{:keys [drawing setLocationChanged] :as props}]
  (let [map-ref (hh/use-ref nil)
        location  (log/spy (use-sub [:yard/location]))
        zoom  (log/spy (use-sub [:yard/zoom]))
        polygons (use-sub [:yard/polygons])
        camera (hh/use-memo
                [location zoom]
                (util/to-js
                 {:center location
                  :zoom zoom
                  :pitch 0
                  :heading 0}))]
    ($ rn/View {:style {:flex 1}}
       ($ MapView
          {:ref map-ref
           :camera  (log/spy camera)
           :provider "google"
           :mapType "hybrid"
           :onRegionChangeComplete
           (fn [event options]
             (let [{:keys [is-gesture]} (util/from-js options)]
               (when is-gesture
                 (-> ^js (.getCamera map-ref.current)
                     (.then (fn [camera]
                              (let [{center :center
                                     zoom :zoom} (util/from-js camera)]
                                (dispatch [:yard/set-location center])
                                (dispatch [:yard/set-zoom zoom])
                                (setLocationChanged true))))))))
           :style (j/get styles :map)}
          (map-indexed (fn [i coordinates]
                         ($ Polygon {:key (str i)
                                     :coordinates (clj->js coordinates)
                                     :strokeWidth 2
                                     :strokeColor "green"
                                     :fillColor "rgba(0,128,0,0.5)"}))
                       polygons))
       (when drawing
         ($ Canvas {:mapRef map-ref})))))
I am sorry I didn’t provide more details earlier.

rayat 2024-04-01T18:02:37.423019Z

ah crap, have no experience with react native, does that need or use the key prop?

hadils 2024-04-01T18:02:48.132359Z

Yes, it does.

rafaeldelboni 2024-04-01T18:07:53.993659Z

It looks like the Polygon is ignoring the :key input, but I don't have experience with react native neither. Do you have an dummy / empty component that you could use, that you sure it handles the :key?

hadils 2024-04-01T18:11:48.487919Z

Let me see.