Fork me on GitHub

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?


Have you checked current-component, props and children?




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. 😕


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


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