Last active
September 6, 2019 15:28
-
-
Save frankiesardo/c81edb2de16c5d869810f6a595e56b64 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns react.helper | |
#?(:cljs (:require [cljsjs.react] | |
[cljsjs.create-react-class]))) | |
#?(:cljs | |
(defn- react-factory [display-name render] | |
(js/React.createFactory | |
(js/createReactClass | |
#js {:displayName display-name | |
:shouldComponentUpdate | |
(fn [next-props _] | |
(this-as this | |
(not= (.. this -props -value) (.-value next-props)))) | |
:render | |
(fn [] | |
(this-as this | |
(apply render (-> this .-props .-value))))})))) | |
#?(:cljs | |
(defn component [display-name render] | |
(let [factory (react-factory display-name render)] | |
(fn create-element [& props] (factory #js {:value props}))))) | |
#?(:cljs | |
(defn $ [el & args] | |
(let [[opts children] (if (map? (first args)) | |
[(clj->js (first args)) (rest args)] | |
[#js {} args])] | |
(apply js/React.createElement el opts children)))) | |
#?(:clj | |
(defmacro defc [name argv & body] | |
`(def ~name (react.helper/component (name '~name) (fn ~argv ~@body))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment