From aa1b090ca336843dca66fe25db05b653dc333775 Mon Sep 17 00:00:00 2001 From: Will Acton Date: Tue, 26 Mar 2019 12:11:15 -0700 Subject: [PATCH] fix double-calling HoCs and also don't wrap them in a list --- examples/workshop/core.cljs | 6 ++++-- src/hx/react.clj | 23 ++++++++++------------- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/examples/workshop/core.cljs b/examples/workshop/core.cljs index 17810ca..00758d5 100644 --- a/examples/workshop/core.cljs +++ b/examples/workshop/core.cljs @@ -244,7 +244,7 @@ hoc)) (defn hoc-example2 [color] - (fn [c] + (fn example2 [c] (let [hoc (hx/fnc Example [_] [:div {:style {:color color}} [c]])] (set! (.-displayName hoc) (str "Color(" (.-displayName c) ")")) @@ -252,7 +252,9 @@ (hx/defnc HoCWrapper [_] {:wrap [hoc-example - (hoc-example2 "green")]} + (react/memo =) + ((hoc-example2 "green")) + ]} [:span "HoC should be header and green"]) (dc/defcard hoc-wrapper diff --git a/src/hx/react.clj b/src/hx/react.clj index 0d7cc6f..8b9ff20 100644 --- a/src/hx/react.clj +++ b/src/hx/react.clj @@ -39,13 +39,15 @@ (map (fn [x] `(assert ~x)) (:pre (first body)))) (hx.react/parse-body ;; post-conditions - ~(if (and opts-map? (:post (first body))) - ;; save hiccup value of body - `(let [~ret (do ~@(rest body))] - ;; apply post-conditions - ~@(map (fn [x] `(assert ~(replace {'% ret} x))) - (:post (first body))) - ~ret) + ~(if opts-map? + (if (:post (first body)) + ;; save hiccup value of body + `(let [~ret (do ~@(rest body))] + ;; apply post-conditions + ~@(map (fn [x] `(assert ~(replace {'% ret} x))) + (:post (first body))) + ~ret) + `(do ~@(rest body))) ;; if no post-conditions, do nothing `(do ~@body))))))) @@ -60,12 +62,7 @@ (when js/goog.DEBUG (set! (.-displayName ~wrapped-name) ~(str *ns* "/" display-name))) (def ~display-name (-> ~wrapped-name - ~@(-> body first :wrap - ;; wrap as a list so that you can do - ;; like `[hocA (returns-hocB some-config)]` - ;; and `(returns-hocB some-config)` will be - ;; called to return an HoC - (as-> w (map list w))))))) + ~@(-> body first :wrap))))) `(do (def ~display-name ~(fnc* display-name props-bindings body)) (when js/goog.DEBUG