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

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.