Last active
August 29, 2015 14:24
-
-
Save bhauman/b39cfad51c9d48037b69 to your computer and use it in GitHub Desktop.
cljs React Hot loader - keeping local state stable across React class changes
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 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