Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

clojurescript/reagent for function doesn't work

Im recently using reagent and re-frame for my clojurescript project and i have a problem: So i have html custom tags

<question id="1"></question>
<question id="2"></question>

And i want to swap them into my reagent-generated html using cljs for function

(defn mypanel []
 [:p "Hi!"])

(let [q (.getElementsByTagName js/document "question")]
  (for [i (range 2)]
    ^{:keys i}
    (reagent/render [mypanel]
                  (aget (.getElementsByTagName js/document "question") i))))

But it doesn't work, i tried to test it without using the for function by

(reagent/render [mypanel]
     (aget (.getElementsByTagName js/document "question") 0))

and it worked just fine with only one tag.

And i don't know why the for function doesn't work, or does reagent doesn't work that way? Anybody have a suggestion?

I'm very noob at this.

like image 970
Nyoman Arie Pranasakti Avatar asked Mar 13 '23 02:03

Nyoman Arie Pranasakti


1 Answers

for produces a lazy sequence, which means that none of the work to evaluate the sequence is done until it is required. You can't use lazy sequences to force side effects, as they will never be evaluated (render is one such place). To force side effects you should probably replace it with doseq. And in your case dotimes would probably be better:

(let [q (.getElementsByTagName js/document "question")]
  (dotimes [i 2]
    ^{:keys i}
    (reagent/render [mypanel]
                  (aget (.getElementsByTagName js/document "question") i))))
like image 75
leetwinski Avatar answered Mar 20 '23 02:03

leetwinski