This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2020-06-07
Channels
- # announcements (1)
- # babashka (79)
- # beginners (177)
- # cider (27)
- # cljdoc (24)
- # cljs-dev (4)
- # clojure (43)
- # clojure-norway (7)
- # clojure-uk (3)
- # clojurescript (52)
- # conjure (12)
- # cryogen (1)
- # cursive (5)
- # data-science (7)
- # datahike (1)
- # datomic (1)
- # dirac (2)
- # helix (14)
- # jobs-discuss (130)
- # juxt (4)
- # liberator (4)
- # malli (2)
- # mxnet (2)
- # news-and-articles (1)
- # nrepl (19)
- # off-topic (27)
- # pathom (3)
- # practicalli (1)
- # re-frame (4)
- # reagent (2)
- # ring (7)
- # shadow-cljs (21)
- # spacemacs (11)
- # vim (38)
- # xml (2)
- # xtdb (17)
I’m going through one of the tutorials and don’t understand why in the code snippet below you need the txt
as in argument to the function inside of let in order for state change to work.
When I print out the value of txt
, I get nil; so I am confused what its purpose is.
(defn home-page []
(let [state (reagent/atom 0)]
(fn [txt]
[:div
(println txt)
[:h1 {:style {:color (when txt "green")}} "Hello!"]
[:p "This is my message."]
[:button.green
{:on-click #(swap! state inc)}
(str txt " " @state)]])))
(defn home-page [txt] ...
needs to match the (fn [txt] ...
args. for the first invocation the arguments are first passed to home-page
and reagent will then call the inner function with the same args. on update only the inner function is called.
But the result is the same regardless. The function it prints nil
as the value on each iteration; however, the UI display the correct value.
@tiotolstoy You can prefix the outer unused arg with _
to make clj-kondo ignore it
@tiotolstoy how are you calling it? [home-page "hello world"]
should have txt
as "hello world"
The example is from this guide https://purelyfunctional.tv/guide/reagent/
@tiotolstoy read this: https://github.com/reagent-project/reagent/blob/master/doc/CreatingReagentComponents.md#form-2--a-function-returning-a-function and then the section under "rookie mistake"
I read that. I’m not sure I understand why you need it in the inner function since state is held in let
line.
@tiotolstoy there are two kinds of information that are used in a component: arguments and data from reactive atoms. both make the component change when the data changes.
so if you don't pass in the argument, or the arguments don't line up somehow in the form-2 component, nil is expected
@tiotolstoy you didn't answer HOW you are using it. [home-page]
will have txt
as nil
.
I’m calling it from the router
(def router
(reitit/router
[["/" :index]
["/items"
["" :items]
["/:item-id" :item]]
["/about" :about]]))
(defn page-for [route]
(case route
:index #'home-page
:about #'about-page
:items #'items-page
:item #'item-page))
However; when I remove the args; the state change does not work; despite txt
not being used.
I’m just wondering what function txt
serves as no args are being passed in; and yet I can’t do away with it.
So I am looking at this function thinking it might be better as a reduce
?
(defn set-in [m [k & ks] v]
(doto m (obj/set k (if (empty? ks) v (set-in (obj/get m k #js{}) ks v))))
@U2FRKM4TW looking to avoid additional external dependencies
Just in case that's because of the bundle size - cljs-oops should be fine since it's just a collection of macros.
since you're mutating it you can use getValueByKeys
I think?
(defn set-in [o ks v]
(obj/set
(obj/getValueByKeys (to-array (drop-last ks))
(last ks)
v)
o)
the downside here is that it traverses ks
twice and there's some unnecessary allocation of seqs but
it's a bit more straight forward. you could probably write a helper fn to give you the last element w/ one traversal
(defn split-last
([xs] (split-last #js [] xs))
([arr [x & xs]]
(if (seq xs)
(recur (doto arr (.push x))
xs)
#js [arr x])))
@lilactown thanks thats helpful