From 6b68815f246d900e1b1f593487779f3ecef64767 Mon Sep 17 00:00:00 2001 From: Juho Teperi Date: Fri, 28 Feb 2020 16:58:33 +0200 Subject: [PATCH] Ensure render sees the up-to-date props --- src/reagent/impl/component.cljs | 20 ++++++++++++-------- src/reagent/ratom.cljs | 3 +-- test/reagenttest/testreagent.cljs | 7 +++---- 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/src/reagent/impl/component.cljs b/src/reagent/impl/component.cljs index 1f3b49e..be310f6 100644 --- a/src/reagent/impl/component.cljs +++ b/src/reagent/impl/component.cljs @@ -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 diff --git a/src/reagent/ratom.cljs b/src/reagent/ratom.cljs index fe396d2..c9129b9 100644 --- a/src/reagent/ratom.cljs +++ b/src/reagent/ratom.cljs @@ -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))) diff --git a/test/reagenttest/testreagent.cljs b/test/reagenttest/testreagent.cljs index 25a758d..a5df8fe 100644 --- a/test/reagenttest/testreagent.cljs +++ b/test/reagenttest/testreagent.cljs @@ -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))