Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@frankiesardo
Last active September 6, 2019 15:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frankiesardo/c81edb2de16c5d869810f6a595e56b64 to your computer and use it in GitHub Desktop.
Save frankiesardo/c81edb2de16c5d869810f6a595e56b64 to your computer and use it in GitHub Desktop.
(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