This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2019-09-26
Channels
- # announcements (2)
- # babashka (9)
- # beginners (95)
- # calva (4)
- # circleci (2)
- # clj-kondo (5)
- # clojure (57)
- # clojure-berlin (2)
- # clojure-conj (3)
- # clojure-europe (6)
- # clojure-italy (14)
- # clojure-nl (3)
- # clojure-switzerland (5)
- # clojure-uk (32)
- # clojuredesign-podcast (5)
- # clojurescript (29)
- # clojutre (16)
- # code-reviews (6)
- # data-science (6)
- # datomic (9)
- # fulcro (33)
- # graalvm (2)
- # jobs (1)
- # jvm (1)
- # kaocha (6)
- # leiningen (4)
- # off-topic (3)
- # re-frame (31)
- # reagent (16)
- # reitit (22)
- # remote-jobs (2)
- # shadow-cljs (70)
- # spacemacs (19)
- # sql (9)
- # tools-deps (13)
- # xtdb (2)
- # yada (3)
Is there a best practice to pass CSS and HTML attributes to reagent components? Currently, I am doing it like below to provide some overwritable defaults
(defn Component
""
[]
(reagent/with-let
[props (reagent/props (reagent/current-component))
children (reagent/children (reagent/current-component))]
(into [:div
{:style (merge {:background "red"}
(:style props))}]
children)))
[Component {:style {:background "blue"}}]
@timrichardt there isn’t anything enforced, but I always try and have my component take in a props map as the first argument
the way you’re doing it here, though, is:
1. Incorrect - it will only ever get the props
/ children
on the first render. it won’t react to new props because you are using with-let
2. more complicated then it needs to be
here’s how I would do it:
(defn component
[{:keys [style] :as props} & children]
(into [:div {:style (merge {:background "red"} style)}]
children))
This is how I am doing it right now, I would like to have the ability to omit props and children without the hassle of the multi-arity definitions.
Could be automated by a macro, but I prefer to have something more native.
And I am not sure which variant results in less code/compile time.
ultimately this is a problem with how reagent treats arguments, as they are like an fn
rather than a props collection like raw React
using a library that binds more closely to React, like hx
, you can easily do what you’re talking about because the hiccup parser has to understand the difference between props and children at parse time
Thanks for the hint.