Search code examples
clojurescriptreagent

How to set array as state in Reagent using ClojureScript


I'm trying to create a state called items however my code gives an error and I'm unsure of why when I try to access the items and iterate over them. What am I doing wrong here?

(def items (r/atom ["test" "test2"]))

(defn home-page []  
  [:div#main
    [:section.section
      [:h1#s-one-greeting "Hello, I'm testing"]
      [:h2#s-one-greeting-two "blah blah blah"]]
    [:section.section
      [:p "Work history"]
      [:p "yada"]
      [:p "yada"]
      [:ul
        (for [item items]
          ^{:key item} [:li "item " item])]]])

Solution

  • You are accessing items without derefing it. So you are trying to for loop over the reagent atom which doesn't work. Just switch it to (for [item @items] ...) and you should be fine.