ClojureScript函数可打印字符串,但不会返回打ic [英] ClojureScript function prints strings, but will not return hiccup

查看:91
本文介绍了ClojureScript函数可打印字符串,但不会返回打ic的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ClojureScript组件:

I have a ClojureScript component:

(defn main-panel []

  (def nodes (-> @(re-frame/subscribe [::subs/nodes])))

  (defn list-nodes [node]
    (prn (node :name)))

  (reagent/create-class
   {:component-did-mount
    (fn []
      (api-service/get-file-tree))

    :reagent-render
    (fn []
      (doseq [node nodes]
        (if (= (node :depth) 0)
          (list-nodes node))))}))

将字符串打印到控制台.

which prints strings to the console.

但是当我将第一个功能更改为:

But when I change the first function to:

  (defn list-nodes [node]
    [:<>
     [:h1 (node :name)]])

我没有得到任何呈现的html-没有错误,浏览器窗口保持空白.

I don't get any html that is rendered - no errors and the browser window stays blank.

如何更改此设置以使其呈现html?预先感谢.

How can I change this so it renders html? Thanks in advance.

为回答以下问题,我将doseq更改为for. 现在的问题是,我收到错误Uncaught Invariant Violation: Objects are not valid as a React child,如果您正在使用React,这很好,因为您可以调试,但是可以使用试剂,而没有那么多.考虑以下.此功能:

In response to the answer below I have changed doseq for for. The issue is now that I am getting the error Uncaught Invariant Violation: Objects are not valid as a React child which is fine if you're working with React because you can debug, but with reagent, not so much. consider the following. This function:

(defn node [nodes]
  (prn (nodes :name)))

由该函数调用时:

    :reagent-render
    (fn []
      (for [nodes all-nodes]
        (if (= (nodes :depth) 0)
          (do
            (nodeComponent/node nodes)
            ))))

打印5个字符串. 但是将调用的函数更改为此:

prints 5 strings. But changing the called function to this:

(defn node [nodes]
  [:h1 (nodes :name)])

导致此错误:Uncaught Invariant Violation: Objects are not valid as a React child.

此问题是否与另一个问题有关?在吃吗?预先感谢.

Is this problem somehow related to another problem I'm having? Thanks in advance.

推荐答案

对于其中一个,defdefn创建全局绑定.为了创建本地绑定,请使用letletfn:

For one, def and defn create global bindings. In order to create local bindings, use let or letfn:

(defn main-panel []
  (let [nodes (-> @(re-frame/subscribe [::subs/nodes]))
    (letfn [(list-nodes [node]
              (prn (node :name)))]
      (reagent/create-class
       {:component-did-mount
        (fn []
          (api-service/get-file-tree))
        :reagent-render
        (fn []
          (doseq [node nodes]
            (if (= (node :depth) 0)
              (list-nodes node))))}))

您的问题是render函数应该返回打ic形式,但是doseq返回nil.内部函数运行(这就是为什么您看到prn的控制台输出的原因),但是它的返回值却被丢弃了(这就是为什么您看不到HTML的原因).即时解决方法可能是将doseq替换为for,但

Your problem is that the render function should return the hiccup forms, but doseq returns nil. The function inside gets run (that's why you see console output from prn), but its return value is thrown away (that's why you see no HTML). An immediate fix might be to replace doseq with for, but

您也许可以这样做(第一种试剂形式):

You could maybe do it like this (first reagent form):

(defn main-panel []
  [:ul (for [node @(re-frame/subscribe [::subs/nodes])
             :when (zero? (:depth node))]
         ^{:key (:id node)} ; whatever identifies nodes, for performance
         [:li {:class "node"} (:name node)]])

我会在这里启动数据,而不是在您启动应用程序的任何地方触发数据获取(或多或少与初始应用程序渲染步骤并行).

I would trigger data fetching not here but wherever you startup your application (more or less parallel to the initial app render step).

这篇关于ClojureScript函数可打印字符串,但不会返回打ic的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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