7

About reagent.

I need to change some CSS class name dynamically. How should I do that?

Sample code is here.

(defn app []
  (let [array [1, 2, 3]]
    (fn []
      [:div
       (for [index array]
         ;; I wanna change this classname like `item-1, item-2, ...`
         ^{:key index} [:div.i-wanna-change-this-classname-dynamically index])])))
mitsuruog
  • 1,259
  • 3
  • 12
  • 21

1 Answers1

11

Change

[:div.i-wanna-change-this-classname-dynamically index]

to

[:div {:class (str “item-” index)} index]

Reagent provides a shorthand syntax of :div.class1.class2#id, but you can also set these in a map as the first item in the vector after :div.

Also keep in mind the CSS :nth-child() selector as another option for dynamic styling.

Daniel Compton
  • 13,878
  • 4
  • 40
  • 60