reagent

A minimalistic ClojureScript interface to React.js http://reagent-project.github.io/
Jacob Emcken 2020-07-06T16:02:09.325900Z

I find my self keep wanting to have the index available when doing for loops (when they are based of a list or vector:

(def choices ["A" "B" "C"])

[:select {:on-change #(js/console.log (.-value (.-target %))}
 (for [choice choices]
   [:option {:value index} choice])] ; <- how do I get the index?

Jacob Emcken 2020-07-06T16:04:13.327600Z

I've handled it before by changing choices to a map: {0 "A", 1 "B", 2 "C"} or using map for the iteration. But the for loop looks soo much better. Is the some "secret" that I'm overlooking?

2020-07-06T16:04:35.327700Z

1. for is not a loop 2. the canonical idiom is (for [[idx choice] (map-indexed vector choices)] ...)

❤️ 1
Jacob Emcken 2020-07-06T16:07:15.327900Z

nice one thanks

2020-07-06T17:39:14.328900Z

Well, it is better if you can get a unique id :) for the react key

2020-07-06T17:40:21.329200Z

right - but the key should be tied to the identity of the item, not the order

💯 1