Skip to content

Instantly share code, notes, and snippets.

@bhauman
Last active August 29, 2015 14:24
Show Gist options
  • Save bhauman/b39cfad51c9d48037b69 to your computer and use it in GitHub Desktop.
Save bhauman/b39cfad51c9d48037b69 to your computer and use it in GitHub Desktop.
cljs React Hot loader - keeping local state stable across React class changes
(ns cljs-react-reload.core
(:require
[cljs.env :as env]))
(defmacro def-react-class [vname body]
`(def ~vname (js/React.createClass ~body)))
(defmacro defonce-react-class
"Patches stable base react class on reload."
[vname body]
(if (and env/*compiler*
(let [{:keys [optimizations]} (get env/*compiler* :options)]
(or (nil? optimizations) (= optimizations :none))))
`(let [base# ~body]
(defonce ~vname (js/React.createClass base#))
(doseq [property# (map
name
'(shouldComponentUpdate
componentWillReceiveProps
componentWillMount
componentDidMount
componentWillUpdate
componentDidUpdate
componentWillUnmount
render))]
(when (aget base# property#)
(aset (.-prototype ~vname) property# (aget base# property#)))))
`(defonce ~vname (js/React.createClass ~body))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment