Skip to content

Instantly share code, notes, and snippets.

@piranha
Created June 29, 2016 15:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save piranha/79e2b4ffd91178c871911fbda716a387 to your computer and use it in GitHub Desktop.
Save piranha/79e2b4ffd91178c871911fbda716a387 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