This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2020-02-27
Channels
- # announcements (8)
- # architecture (3)
- # aws (18)
- # beginners (96)
- # bristol-clojurians (3)
- # calva (15)
- # cider (7)
- # clj-kondo (8)
- # clojure (135)
- # clojure-denmark (1)
- # clojure-dev (14)
- # clojure-europe (37)
- # clojure-italy (9)
- # clojure-nl (14)
- # clojure-sanfrancisco (1)
- # clojure-spec (1)
- # clojure-uk (54)
- # clojurescript (27)
- # core-async (243)
- # cursive (28)
- # data-science (6)
- # datomic (33)
- # fulcro (25)
- # graalvm (24)
- # hoplon (2)
- # instaparse (12)
- # jackdaw (1)
- # java (21)
- # juxt (12)
- # meander (10)
- # nyc (4)
- # off-topic (6)
- # om (3)
- # pathom (17)
- # perun (1)
- # re-frame (29)
- # reitit (4)
- # rum (3)
- # shadow-cljs (119)
- # spacemacs (31)
- # xtdb (14)
When an event needs data from the db
are there any negatives of accessing the db
directly in the even vs using a dedicated subscriber?
Not sure what the question is about. Do you mean using a subscription value to create an event vector so that it can be used in the corresponding event handler to avoid making it go look into the DB?
and just to clear up, do you mean subscribe to the sub inside the event or have it passed in to the event dispatch?
I mean subscribe to the sub or inject it into the coeffects of the event.
Using a sub in an event handler is possible (https://github.com/den1k/re-frame-utils/blob/master/src/vimsical/re_frame/cofx/inject.cljc). But if the sub is not used anywhere else right at that moment then you don't gain any performance at all, you lose it.
I just wanted to make sure that there were no dangers in accessing the db directly in the event since most of the time the way to get values from the db is through subscribers. Sounds like it's actually the preferred approach to access the db directly.
Event handlers receive DB as one of the co-effects. They are designed for you to look into the DB directly.
Thanks for the clarification and break down! Much appreciated.
this won't do "the right thing", correct?
(defn item-list
[]
(let [items (rf/subscribe [:items])]
[:div
[:h3 "items:"]
[:ul
(map item-list-entry @items)]]))
the generated hiccup won't contain any ratoms so reagent won't know to update the component when the underlying data changes. the proper thing would be to subscribe in the parent component and generate [item-list @items]
-- correct?@mbarillier that item-list
will update correctly
however, passing in the items as props might be better if you intend to re-use this component to display multiple states
you also probably want something more like (assuming item-list-entry
is also a Reagent component):
[:ul
(for [item @items]
[item-list-entry item])]
so that if item-list-entry
dereferences any atoms, they will also re-render correctlyI’m assuming you’re using this as [item-list]
. if you call it like a function, (item-list)
then you’re right it might not re-render correctly
@lilactown the intent is to have the ul
update as the items change (single instance of the component). assuming [item-list-entry ITEM]
generates [:li "..."]
then reagent won't know that [item-list]
would have to be refreshed when the data under the sub changes -- or is there some magic I'm missing? the atom used in the for
/`map` expression won't be seen by reagent in the generated list hiccup.
it’s not about the generated hiccup. the atom only needs to be dereferenced when the function is executed
it’s not the ul
that is updated; the whole function will be executed when the atom changes
then reagent will convert the hiccup to React Elements, and React will diff between what was last rendered and the new element tree, and update the ul
in the DOM
[:ul
(for [item-id @item-ids]
[item-list-entry item-id])]
(There will be less component re-rendering this way)(note to self: don't ever add anything like that to own libraries that others might use)
The thing is, it is in the docs! https://github.com/reagent-project/reagent/blob/master/doc/ReactFeatures.md and https://cljdoc.org/d/reagent/reagent/0.9.1/doc/tutorials/react-features
The problem is that it's *impossible* to find via any search engine. You have to either open every single documentation page and just incessantly Ctrl-F
it to hell, or just download Reagent sources and search through there (which was exactly what I did myself back when I was still getting started with it).
http://reagent-project.github.io/ not in these docs