:on-click渲染为文本 [英] :on-click renders as text

查看:89
本文介绍了:on-click渲染为文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的ClojureScript函数:

My ClojureScript function:

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

在dom中以html文本形式呈现:

renders as html text in the dom:

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

我的代码看起来与我在网上找到的:on-click示例完全相同.

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?

谢谢.

推荐答案

我不确定项目其余部分的结构如何,但看起来像您尝试使用的div的属性映射和内部html进行转置.

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.

呈现的模型以这种方式出现的原因是,试剂发现:div向量的第一个子代不是映射(大概是String),并将所有剩余的子代解释为预期的其他子代. html中的div,对它们进行字符串化处理.

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)])


我删除了:<>位,因为在此组件中只有一个div,因此在这里不需要它.


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

我将node:name换成了更加零安全的名称,并更清楚地查找名称.

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

这篇关于:on-click渲染为文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆