A minimalistic ClojureScript interface to React.js
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?


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

Jacob Emcken 2020-07-06T16:07:15.327900Z

nice one thanks


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


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