Search code examples
clojurereactjsclojurescriptreagent

Reagent does not register atom watch


I have simple newcomer's luminus with reagent application. I defined some component definition:

(def md-vals (atom nil))

(defn markdown [url]
  (GET
    (str js/context url)
    {:handler
     (fn [response]
       (swap! md-vals assoc url response)
       (js/alert (str "loaded\n" (subs response 1 100))))})
  (fn [] [:p (get @md-vals url "loading...")]))

It's usage as page component:

(defn docs [] [markdown "/md/docs.md"])

And finally page binding:

(defn init! []
  ...
  (render-component (docs)  (.getElementById js.document "docs")))

All works fine except atom state switch notification. At the time of successfull resource get i'm seeing alert message as supposed, but corresponding area of page does not switching.

Chrome debugger shows that at the moment of atom switch atom's watches property is empty (null), so no notification is fired.

What could probably be wrong in my approach?


Solution

  • You should use reagent.core/atom instead of regular Clojure atom. Add this to your namespace :require:

    [reagent.core :as reagent :refer [atom]]