user31415926 user31415926 - 1 year ago 116
React JSX Question

Purpose of : "^{:key item}"

I'm reading

A snippet of code there is:

(defn lister [items]
(for [item items]
^{:key item} [:li "Item " item])])

What is the meaning of the ^{:key item} and why do we want it?
[The documentation refers to making Reflect faster.]

Answer Source

React needs to identify the individual list items, so that it can tell which ones have changed. Otherwise, React can only throw away the entire list when rerendering. Therefore, you need to add a key.

In React (JSX):

<li key={item.whatever}>

In reagent, you add the key as metadata to the Hiccup form. Reagent takes care of setting the key for React then.

^{:key (.-whatever item)} [:li (.-bar item)]

You can use anything uniquely identifying the content of the list element. In ClojureScript/Reagent, you usually use the item itself.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download