Ensure render sees the up-to-date props

This commit is contained in:
Juho Teperi 2020-02-28 16:58:33 +02:00
parent daab43d6e2
commit 6b68815f24
3 changed files with 16 additions and 14 deletions

View File

@ -399,8 +399,6 @@
comp
(as-class comp)))
(defonce fun-component-state #js {})
(defn functional-wrap-render
[^clj c argv]
(let [f (.-reagentRender c)
@ -416,19 +414,21 @@
(recur c argv))
:else res)))
(defn functional-do-render [c argv]
(defn functional-do-render [c]
(binding [*current-component* c]
(if (dev?)
;; Log errors, without using try/catch (and mess up call stack)
(let [ok (array false)]
(try
(let [res (functional-wrap-render c argv)]
(let [res (functional-wrap-render c (.-argv c))]
(aset ok 0 true)
res)
(finally
(when-not (aget ok 0)
(error (str "Error rendering component" (comp-name)))))))
(functional-wrap-render c argv))))
(functional-wrap-render c (.-argv c)))))
(def fun-component-state #js {})
(defn functional-render [jsprops]
(let [argv (.-argv jsprops)
@ -465,7 +465,10 @@
;; constructor refers to the original fn.
:reagentRender tag
:constructor tag
:cljsIsDirty false}]
:cljsIsDirty false
;; Argv is also stored in the state,
;; so reaction fn will always see the latest value.
:argv argv}]
(gobj/set fun-component-state id obj)
obj))]
@ -479,19 +482,20 @@
(assert-callable tag)
(batch/mark-rendered reagent-state)
(set! (.-argv reagent-state) argv)
;; static-fns :render
(if-let [rat (gobj/get reagent-state "cljsRatom")]
(._run rat false)
(ratom/run-in-reaction
;; Mock Class component API
#(functional-do-render reagent-state argv)
#(functional-do-render reagent-state)
reagent-state
"cljsRatom"
batch/queue-render
rat-opts))))))
(defonce fun-components #js {})
(def fun-components #js {})
(defn funtional-render-fn
"Create copy of functional-render with displayName set to name of the

View File

@ -338,8 +338,7 @@
(defn with-let-values [key]
(if-some [c *ratom-context*]
(cached-reaction array c key
nil with-let-destroy)
(cached-reaction array c key nil with-let-destroy)
(array)))

View File

@ -145,8 +145,7 @@
v2 (r/atom 0)
c2 (fn [{val :val}]
(swap! ran inc)
;; FIXME:
; (is (= val @v1))
(is (= val @v1))
[:div @v2])
c1 (fn []
(swap! ran inc)
@ -663,9 +662,9 @@
[:div "" (reset! spy @(r/track t1))])]
(with-mounted-component [c2]
(fn [c div]
;; FIXME:
; (is (= {:v1 1 :v2 1} @v))
(is (= {:v1 1 :v2 1} @v))
;; FIXME:
; (r/force-update (:c2 @comps))
; (is (= {:v1 1 :v2 2} @v))