reagent-project / reagent

A minimalistic ClojureScript interface to React.js
http://reagent-project.github.io/
MIT License
4.75k stars 414 forks source link

Should Reagent re-render after clicking the button in this snippet? #50

Closed borkdude closed 9 years ago

borkdude commented 9 years ago

The following does not seem to work. I'm fairly new to Reagent, so my question is, should it work?

(def show? (atom true))

(defn ^:export run []
  (reagent/render-component
   [:div (str @show?)
    [:button {:on-click
              (fn [e] (swap! show? not))}
     "swap"]]
   (.-body js/document)))
mainej commented 9 years ago

I think reagent wants components to be functions, so that it can re-evaluate them when atoms change:

(def show? (atom true))

(defn show-div []
  [:div (str @show?)
   [:button {:on-click
             (fn [e] (swap! show? not))}
    "swap"]])

(defn ^:export run []
  (reagent/render-component
    [show-div]
    (.-body js/document)))