问题
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