This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2021-01-29
Channels
- # announcements (13)
- # aws-lambda (19)
- # babashka (11)
- # beginners (90)
- # calva (35)
- # chlorine-clover (16)
- # cider (8)
- # clj-kondo (6)
- # cljsrn (10)
- # clojure (44)
- # clojure-australia (1)
- # clojure-europe (35)
- # clojure-nl (6)
- # clojure-norway (7)
- # clojure-uk (101)
- # clojurescript (79)
- # community-development (40)
- # conjure (1)
- # contributions-welcome (4)
- # core-logic (2)
- # cursive (6)
- # datomic (4)
- # figwheel-main (5)
- # fulcro (18)
- # girouette (1)
- # graalvm (3)
- # hoplon (2)
- # hugsql (2)
- # jobs (5)
- # malli (26)
- # off-topic (74)
- # pathom (36)
- # portal (4)
- # reagent (10)
- # reitit (23)
- # remote-jobs (1)
- # shadow-cljs (27)
- # spacemacs (10)
- # sql (3)
- # startup-in-a-month (6)
- # tools-deps (55)
- # vim (11)
- # xtdb (15)
Hi all, is this a bug or expected behaviour? I'm expecting props=nil
children=["Bar"]
instead of props="Bar"
and children=nil
I thought, props will have value only when second element in hiccup vector is a map?
; Given I have component
(defn foo [props children]
[:div props children])
And I render like
[foo "Bar"]
It is expected. You have created a component that requires two arguments but you have provided only one.
Thanks for confirming. I'm trying to create text
component with api like:
[text {:variant :primary
:style {:some "style"}}
"Some Text"]
[text "Hey"]
Is there a way I can improve below code?
(def text-styles {:variants {:primary {:color (value :colors :primary :700)}}})
(defn text []
(let [this (r/current-component)
{:keys [variant style]
:or {variant :default}} (r/props this)]
(into [:> rn/Text {:style (merge
(variant-style text-styles variant)
style)}] (r/children this))))
I'm still not sure what you're after. You say you're trying to create a component with two arguments. But your example has a component with no arguments. My brain cannot magically reconcile the two. If you want optional arguments, you can use multi-arities or a map with optional keys. That's all I can say right now.
Sorry for the confusion... "If you want optional arguments, you can use multi-arities or a map with optional keys. That's all I can say right now." Actually the multi arity is what I'm trying to avoid, because then I have to check if the first param is map (meaning its a props), otherwise its a child, and if second param also has value, then I have to combine first and second param as child. Lets say if I made it this way..
(defn text [props & children])
;; props is "Hey", children is nil
[text "Hey"]
;; props is map, children is (("Hey"))
[text {:style {:fontSize 16}} "Hey"]
;; props is map, children is ([text "Hey"])
[text {:style {:fontSize 16}} [text "Hey"]]
;; props is [text "Child 1"], children is ([text "Child 2"])
[text [text "Child 1"] [text "Child 2"]]
;; props is map, children list of [text]
[text
{:style {:color "red"}}
[text "with color Child 1"]
[text "with color child 2"]]
In the above example, I have to handle inconsistency of the params (props and children) if I will be using the function params. And what I'm trying to point out is, its better not to use the params, and just rely on (r/props) and (r/children) to get consistency. (Consistency here meaning, props will always be a map, and first children will not be in the props
function param in cases I did not pass style map in the component), reagent knows it is a children