试剂组件可以有多个顶级HTML实体吗?

时间:2015-08-04 10:21:58

标签: clojure reagent

我看到很多像这样的代码:

(defn simple-component []
  [:div
   [:p "I am a component!"]
   [:p "This component has two paragraphs"]])

有没有办法让该组件只有两个p元素,没有封闭的div?

2 个答案:

答案 0 :(得分:1)

似乎"菜鸟错误"。 请查看此Wiki页面:Form-1:一个简单的功能 https://github.com/Day8/re-frame/wiki/Creating-Reagent-Components

答案 1 :(得分:0)

您可以使用可通过两种方式创建的react片段来做到这一点:

  1. 带有js数组(带有react元素):

    (defn simple-component []
          #js [(r/as-element [:p "I am a component!"])
               (r/as-element [:p "This component has two paragraphs"])])
    
  2. 带有[:<> ... ]标签(采用打h形式):

    (defn simple-component []
          [:<>
              [:p "I am a component!"]
              [:p "This component has two paragraphs"]])
    

Implemented in this PR