Fork me on GitHub
#helix
<
2024-03-31
>
hadils22:03:28

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.

rafaeldelboni15:04:03

In the map with the props add an :key i

hadils15:04:21

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

rafaeldelboni15:04:03

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)

hadils16:04:11

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

rafaeldelboni16:04:31

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

rafaeldelboni16:04:13

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

hadils16:04:21

I can try that…Thanks @UMMMKKADU

rayat17:04:08

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

hadils17:04:17

Thanks @U037TPXKBGS. 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.

rayat18:04:37

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

hadils18:04:48

Yes, it does.

rafaeldelboni18:04:53

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?

hadils18:04:48

Let me see.

lilactown17:04:40

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

lilactown17:04:22

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?

lilactown17:04:20

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 $

hadils17:04:52

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

lilactown17:04:06

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

hadils17:04:47

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

lilactown17:04:13

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