Fork me on GitHub
#reagent
<
2020-08-19
>
jazzytomato18:08:25

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:

lilactown18:08:30

@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

lilactown18:08:01

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])]))))

jazzytomato20:08:48

Oooooh of course :face_palm:

ordnungswidrig20:08:48

mapv also can help because a vector relizexsthe sequence.