Add component-path, and use in errors and warnings

This commit is contained in:
Dan Holmsand 2015-08-30 18:14:54 +02:00
parent 57c747b358
commit 10562cfb9d
2 changed files with 26 additions and 3 deletions

View File

@ -288,3 +288,9 @@ the result can be compared with ="
[f & args]
(util/partial-ifn. f args nil))
(defn component-path
;;; Try to return the path of component c. Maybe useful for debugging
;;; and error reporting, but may break with future versions of React.
;;; Thus component-path may return nil.
[c]
(comp/component-path c))

View File

@ -188,7 +188,8 @@
name (str (or (:displayName fun-map)
(fun-name render-fun)))
name' (if (empty? name)
(str (gensym "reagent")) name)
(str (gensym "reagent"))
(clojure.string/replace name #"\$" "."))
fmap (-> fun-map
(assoc :displayName name')
(add-render render-fun name'))]
@ -223,10 +224,26 @@
(util/cache-react-class res res)
f))
(defn component-path [c]
(let [elem (some-> (or (some-> c
(.' :_reactInternalInstance))
c)
(.' :_currentElement))
name (some-> elem
(.' :type)
(.' :displayName))
path (some-> elem
(.' :_owner)
component-path
(str " > "))
res (str path name)]
(when-not (empty? res) res)))
(defn comp-name []
(if (dev?)
(let [n (some-> *current-component*
(.' cljsName))]
(let [c *current-component*
n (or (component-path c)
(some-> c (.' cljsName)))]
(if-not (empty? n)
(str " (in " n ")")
""))