reagent

A minimalistic ClojureScript interface to React.js http://reagent-project.github.io/
jazzytomato 2020-08-19T18:44:25.189600Z

Hey, any idea why this doesn't work? Clicking on the button doesn't update the component

(defn root-component []
  (let [selected (r/atom 0)]
    (fn []
      [:p [:button {:on-click #(reset! selected 1)} "Click me"]
       (map
         (fn [x] (str (= x @selected)))
         [0 1 2])])))
However, this works and update the component correctly:
(defn root-component []
  (let [selected (r/atom 0)]
    (fn []
      [:p [:button {:on-click #(reset! selected 1)} "Click me"]
       (str (map
              (fn [x] (= x @selected))
              [0 1 2]))])))
(note the difference of where I put the str function) :thinking_face:

lilactown 2020-08-19T18:54:30.190400Z

@jazzytomato you’re dereferencing the selected atom inside of a lazy sequence, so reagent isn’t able to capture the dereference and set up its tracking magic for you

lilactown 2020-08-19T18:56:01.191400Z

if you lift the dereference out of the map you should see it work:

New
jazzytomato  11:44 AM

Hey, any idea why this doesn't work? Clicking on the button doesn't update the component

(defn root-component []
  (let [selected (r/atom 0)]
    (fn []
      [:p [:button {:on-click #(reset! selected 1)} "Click me"]
       (let [selected-val @selected]
         (map
           (fn [x] (str (= x selected-val)))
           [0 1 2])]))))

ordnungswidrig 2020-08-27T20:39:48.020800Z

mapv also can help because a vector relizexsthe sequence.

jazzytomato 2020-08-19T20:09:48.192300Z

Oooooh of course :face_palm:

jazzytomato 2020-08-19T20:09:54.192700Z

Thanks 😁