我要自定义反应元素的显示方式,这是代码:
(require ["react" :as react])
(defn react-element? [obj]
(if-let [sym (aget obj "$$typeof")]
(= js/Symbol
(type sym))))
(extend-protocol IPrintWithWriter
object
(-pr-writer [obj writer _]
(js/console.log "hello")
(cond (react-element? obj)
(write-all writer
(-> (js->clj obj)
(dissoc "$$typeof")
str))
:else
(write-all writer (str obj)))))
(react/createElement "h2" nil "Hello, World!")
我希望输出是
{:type "h2", :key nil, :ref nil, :props {:children "Hello, World!"}}}
但它仍会打印出正常的#js元素:
#js {"$$typeof" Symbol(react.element),
:type "h2", :key nil, :ref nil,
:props #js {:children "Hello, World!"}, :_owner nil}
是否可以自定义此行为?
答案 0 :(得分:0)
根本原因是在标准lib打印代码中使用了implements?
而没有使用satisfies?
。这有效地将打印替代限制为非本机类型的内容。 (implements?
不适用于本机类型)。
这可能不是故意的,并且可以通过https://dev.clojure.org/jira/browse/CLJS-2812来解决