Fork me on GitHub
#reagent
<
2020-02-03
>
romdoq11:02:36

When writing simple custom components, is there an idiomatic way of having its params map be optional? Such that that both [my-widget [:span "hi"]] and [my-widget {:class "thing"} [:span "hi"]] would be acceptable. I'm currently implementing such components using a util function, eg.:

(defn my-widget
  [& args]
  (let [[params & content] (utils/args->params-content args)
        params (assoc params :blah "blah")]
    [:div params content]))
But I feel like there's something clever/basic I'm missing? Or is it simply more idiomatic to just require an explicit params map, even if it's empty?

aisamu12:02:09

Have you checked current-component, props and children?

aisamu12:02:18

(`reagent.core`)

romdoq12:02:40

Hmm, good suggestion! IIUC:

(defn my-widget
  [& _]
  (let [props (reagent/props (reagent/current-component))
        children (reagent/children (reagent/current-component))]
    [:div props children]))
Looks a bit nicer, but sidestepping the function's explicit args feels maybe a bit too magical. 😕

aisamu12:02:32

Yup, but it's the "official" way :man-shrugging:... We'd end up there anyway if we wanted to transparently use this component from react, so I'd say it balances out

romdoq12:02:59

That's outside my normal use-case right now but yeah, fair point. Ta