Skip to content

Instantly share code, notes, and snippets.

@ajchemist
Forked from piranha/why_update.cljs
Created July 4, 2016 12:55
Show Gist options
  • Save ajchemist/d1c29159505766abd01ca111da27324e to your computer and use it in GitHub Desktop.
Save ajchemist/d1c29159505766abd01ca111da27324e to your computer and use it in GitHub Desktop.
(ns mk.fe.core.why-update
(:require [cljsjs.react]))
(defn comp-did-update [prev-props prev-state]
(this-as this
(let [name (or (aget this "displayName")
(aget this "constructor" "displayName")
(aget this "constructor" "name"))
prev-state (aget prev-state ":rum/state")
state (aget (.-state this) ":rum/state")]
(when (= prev-state state)
(println name "update was avoidable!")))))
(def orig-create-class js/React.createClass)
(def orig-did-update js/React.Component.prototype.componentDidUpdate)
(def Mixin #js {:componentDidUpdate comp-did-update})
(defn enable! []
(set! js/React.createClass
(fn [obj]
(when (nil? (aget obj "mixins"))
(aset obj "mixins" #js []))
(aset obj "mixins" (.concat #js [Mixin] (aget obj "mixins")))
(.call orig-create-class js/React obj)))
:wow)
(defn disable! []
(set! js/React.createClass orig-create-class))
(defonce much (enable!))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment