bhauman / devcards

Devcards aims to provide a visual REPL experience for ClojureScript
1.53k stars 113 forks source link

dom-node macro does not update on atom change #48

Closed fdserr closed 6 years ago

fdserr commented 8 years ago

The following card shows the issue (include dom-node in the devcards :require-macros and paste-in):

(defcard dom-node-not-updating
  "Seems dom-node doesn't update?"
  (dom-node
    (fn [state node]
      (set! (.-innerHTML node) (str "Click me count: " (:count @state)))
      (set! (.-onclick node) #(swap! state update-in [:count] inc))))
  {:count 0}
  {:inspect-data true
   :watch-atom true})

I have tracked the problem down to defonce-react-class DomComponent here: https://github.com/bhauman/devcards/blob/d3a3000593e5b07827ebc98ffe152f1a6520b486/src/devcards/core.cljs#L314

AFAIK it does not make use of the :data_atom passed to it by defn- dom-node* here: https://github.com/bhauman/devcards/blob/d3a3000593e5b07827ebc98ffe152f1a6520b486/src/devcards/core.cljs#L498

I'm not react-able enough to fix this, sorry =[ (and therefore in great need of dom-node !)

bhauman commented 8 years ago

This is an interesting one. I'm wondering what the behavior should be.

Here is a solution in the meantime:

(def render  (fn [state node]
      (set! (.-innerHTML node) (str "Click me count: " (:count @state)))
      (set! (.-onclick node) #(swap! state update-in [:count] inc))))) 

(defn render-on-change [render-fn] 
   (fn [state node]
        (add-watch state :rendering (fn [_ _ _ _] (render-fn state node)) ;; idempotent
        (render-fn state node)))

(defcard dom-node-updating
  "Seems dom-node doesn't update?"
  (dom-node (render-on-change render)
  {:count 0}
  {:inspect-data true})

The parens may be wrong, this is just a quick sketch.

fdserr commented 8 years ago

Thanks much, that should do the trick for now.

exupero commented 8 years ago

I ran into this too and came up with a similar solution, but I also notice that the history controls don't appear when the state first changes. They show up after I save the file and Figwheel reloads or make a second state change.

bhauman commented 8 years ago

good point, that makes sense

On Fri, Oct 23, 2015 at 7:28 AM, Eric Shull notifications@github.com wrote:

I ran into this too and came up with a similar solution, but I also notice that the history controls don't appear when the state changes. They only show up after I save the file and Figwheel reloads.

— Reply to this email directly or view it on GitHub https://github.com/bhauman/devcards/issues/48#issuecomment-150548123.