Skip to content

Commit

Permalink
Merge pull request #8 from Respo/component-memo
Browse files Browse the repository at this point in the history
add React.memo for components
  • Loading branch information
soyaine committed Sep 1, 2019
2 parents 0ff9a1d + 8e2907c commit baec7a4
Show file tree
Hide file tree
Showing 4 changed files with 180 additions and 2 deletions.
168 changes: 168 additions & 0 deletions calcit.edn

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion macros/reacher/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@
(defmacro defcomp [def-name params & body]
(let [func-name (symbol (str "$comp_" (name def-name)))]
`(do
(defn- ~func-name [~@params] ~@body)
(def ~func-name (reacher.core/react-memo (fn [~@params] ~@body)))
(defn ~def-name [& args#]
(apply react-create-element ~func-name args#)))))
11 changes: 10 additions & 1 deletion src/reacher/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@
["react-dom" :as DOM]
[clojure.string :as string]
[medley.core :as medley]
[applied-science.js-interop :as j])
[applied-science.js-interop :as j]
[clojure.set :as set])
(:require-macros [reacher.core :refer [div]]))

(defn- compare-props [prev-props next-props]
(let [prev-keys (set (js->clj (js/Object.keys prev-props)))
next-keys (set (js->clj (js/Object.keys next-props)))]
(->> (set/union prev-keys next-keys)
(every? (fn [x] (= (j/get prev-props x) (j/get next-props x)))))))

(defn dashed->camel
([x] (dashed->camel "" x false))
([acc piece promoted?]
Expand All @@ -27,6 +34,8 @@

(def react-create-element React/createElement)

(defn react-memo [comp] (React/memo comp compare-props))

(defn transform-props [props]
(let [result (-> (map-upper-case props) (update :style map-upper-case))]
(cljs.core/clj->js result)))
Expand Down
1 change: 1 addition & 0 deletions src/reacher/example/comp/container.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
comp-task
(props)
(let [task (j/get props :task), dispatch! (use-dispatch)]
(comment println "rendering task" task)
(div
{:key (:id task), :style (merge ui/row-parted {:padding "0 8px", :width 320})}
(str (:text task))
Expand Down

0 comments on commit baec7a4

Please sign in to comment.