Fork me on GitHub
#reagent
<
2016-12-18
>
ander15:12:29

Hey everybody. I’m pretty new to Clojure and ClojureScript, really enjoying it so far. I was going through the Reagent introduction and came across this piece of code:

(defn lister [items]
  [:ul
   (for [item items]
     ^{:key item} [:li "Item " item])])
I’ve tried looking up the ^{ } notation, but I can’t seem to find anything about it. Any info or links would be appreciated. Thanks!

henriklundahl16:12:05

@ander It's for adding metadata to the following expression. See http://clojure.org/reference/metadata.

pesterhazy16:12:16

@ander, it's a way to specify the react key for collections

pesterhazy16:12:39

it's an aid that helps react track elements in a collection in its tree-diff algorithm

henriklundahl17:12:41

Instead of using metadata you can add a map as first argument to the component, like this: [:li {:key item} "Item " item].

ander17:12:07

That’s closer to what I’m used to from my JS days. Metadata seems interesting, if a little spooky right now

ander17:12:33

With my whole 10 minutes of reading and playing with it, at least 🙂

pesterhazy17:12:40

the whole metadata thing is a mistake IMO; it's a nice hack but not too necessary really

pesterhazy17:12:13

it's hard to reason about when a collection will retain its metadata

mikethompson19:12:20

@ander it has already been explained to you that it is metadara, but for extra understanding about the process, have a read through this: http://stackoverflow.com/questions/37164091/how-do-i-loop-through-a-subscribed-collection-in-re-frame-and-display-the-data-a/37186230#37186230