:on-click renders as text

孤人 提交于 2019-12-11 15:38:34

问题


My ClojureScript function:

(defn node-function [node]
  [:<>
   [:div (node :name) {:on-click #(prn "hi")}]])

renders as html text in the dom:

My Documents{:on-click #object[Function]}

My code looks exactly the same as :on-click examples I've found online.

Why does the compiler think this is text and not a function?

Thanks.


回答1:


I'm not sure how the rest of your project is structured, but it looks like the property map and inner html of the div you're trying to render are transposed.

The reason that the rendered model appears that way is that reagent sees that the first child of the :div vector is not a map (it is a String, presumably) and interprets all remaining children as intended additional children of the emitted div in the html, stringifying them as it goes.

You're likely looking for a function defined more like this:

(defn node-function [node]
  [:div {:on-click #(prn "hi")} (:name node)])

I removed the :<> bit because it isn't needed here due to there only being one div in this component.

I transposed the node and :name to be a little more nil-safe and to make the lookup of name more clearly a lookup.



来源:https://stackoverflow.com/questions/57906528/on-click-renders-as-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!