This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-08-19
Channels
- # babashka (13)
- # beginners (4)
- # biff (1)
- # cider (15)
- # clerk (1)
- # clojure (18)
- # clojure-europe (10)
- # clojure-nl (1)
- # clojure-uk (1)
- # core-logic (2)
- # core-typed (2)
- # datomic (23)
- # defnpodcast (1)
- # emacs (4)
- # fulcro (25)
- # hyperfiddle (8)
- # music (1)
- # off-topic (21)
- # podcasts-discuss (1)
- # polylith (6)
- # releases (1)
- # squint (19)
- # tools-deps (10)
How would one go about optionally including a map before other parameters in the definition of a variadic function?
Context:
Suppose one wants to call a Reagent component (let's call it component)
in one of two ways, for example:
[component {:class "..."} [:h1 ...] [:p ...] ...]
or
[component [:h1 ...] [:p ...] ...]
In both cases, everything that isn't a map is considered a child of component
.
Defining component
as:
(defn component [{:keys [class]} & args]
[:div
{:class (str class " "
"other classes in default component")}
args])
seems suboptimal, as one would have to pass {}
before other parameters when calling component
(otherwise the :h1
in the second example above won't render).✅ 2
Seems Reagent handles this: https://github.com/reagent-project/reagent/blob/master/doc/InteropWithReact.md#getting-props-and-children-of-current-component
"Specifically, if the first argument to your Reagent function is a map, that is assigned to this.props
of the underlying Reagent component. All other arguments are assigned as children to this.props.children
."
Thus, component
definition could be:
(defn component []
(let [this (r/current-component)]
(into [:div {:class (str (:class (r/props this))
" "
"other classes in default component")}]
(r/children this))))