Fork me on GitHub

@hkjels you can use fragments or you can just use reduce instead of map to create a single vector

parrot 4

What is going on here?

(defn my-comp
  [{:keys [class] :as props} & children]
  (into [:div {:class ["class-one" class]}] children))

; works
[my-comp {} [:div ] [:div ]]

; does not work
[my-comp [:div ] [:div ]]
By “does not work” I mean that I would expect the second to also render the :divs but it does not…


@tkjone in the second version you are passing the first div as the props


For some reason I was thinking that reagent was expecting the first to always be a map (and this would become props) and if not a map everything else would become children


I wish that were the case. The only thing it does that is special with a props map is that it will put a props map on the actual props property and the remainder of the arguments on props.children


How would I go about defining my-comp so I could pass it a map, or not, and behave as I expect?


you could check the type of the props object. if it is a vector then treat it as a child


haha that was what I wanted to do, but for some reason I have not seen this pattern in any example code I reviewed.


I figured this would be pretty common


for some reason reagent code doesn’t often do the “arbitrary children” pattern


the whole technique of treating ordered arguments as children is weird


The common case I can think of is when creating a component library:

(defn para 
  [props & children]
  (into [:p props] children))
In the above example, component libraries will often make it so that the component has sane defaults which would mean that for most case you just need to call
[para "This is a paragraph"]


and then overwriting defaults is allowed by providing a map as the first arg


is the accepted way in reagent to do

(defn para 
  [props & children]
  (if ( = props map)
     ;; stuff 1
     ;; stuff 2


a runtime type check will work reliably. just use (if (not (map? props)) ... ...)


yea that’s probably the best you can do with reagent


Good to know. Yeah, I was working on this and was blown away that after several months of solid reagent I had not come across this. I also read through this (quickly, I admit) and it had me thinking that reagent built in a solution