This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2020-01-10
Channels
- # aleph (1)
- # announcements (21)
- # babashka (3)
- # beginners (98)
- # calva (2)
- # circleci (3)
- # clara (58)
- # clj-kondo (123)
- # cljs-dev (1)
- # cljsrn (7)
- # clojure (162)
- # clojure-europe (2)
- # clojure-finland (7)
- # clojure-italy (5)
- # clojure-nl (6)
- # clojure-sanfrancisco (1)
- # clojure-spec (1)
- # clojure-survey (17)
- # clojure-uk (70)
- # clojuredesign-podcast (2)
- # clojurescript (46)
- # cloverage (5)
- # cursive (2)
- # data-science (22)
- # datascript (1)
- # datomic (60)
- # emacs (3)
- # figwheel-main (1)
- # fulcro (26)
- # graalvm (5)
- # jackdaw (3)
- # leiningen (8)
- # luminus (1)
- # off-topic (8)
- # other-lisps (2)
- # pedestal (27)
- # re-frame (17)
- # reagent (20)
- # reitit (3)
- # shadow-cljs (37)
- # spacemacs (23)
- # sql (69)
- # tools-deps (2)
- # utah-clojurians (9)
- # xtdb (3)
Hey all, I have been uncertain about this for a while now. Can anyone shed some light? Can I create and deref a re-frame subscription inside a re-frame event handler or not? The FAQ entry below says I should not, but I’m not sure if it is outdated info or not. The alternative methods proposed by the FAQ entry seem a bit annoying to me. I’m curious if the approaches shown in the FAQ are what you all are doing. https://github.com/day8/re-frame/blob/master/docs/FAQs/UseASubscriptionInAnEventHandler.md
@harishtella I agree it is annoying. I pass the data in instead
Oh, why didn’t I think of that. As in the reagent component makes the subscription needed, and then passes the value into the event dispatch vector.
That would be one way to handle that, yes. For some situations. Not a universal solution though.
For simple things, I generally grab the data directly from the cofx / app-db. I'm not sure if that's a particularly bad practice or not, though.
It's fine if you're fine with duplicating the logic. Sometimes it's acceptable or even desirable, sometimes it's not.
This is kind of what I had in mind.
(defn search-form
[]
(let [needed-val (rf/subscribe [:my-sub])]
(fn []
[:div#search-form
[:button {:on-click #(rf/dispatch [:my-event @needed-val])
The downside I see is that this component gets updated whenever the :my-sub subscription changes, when it doesn’t really need to.
@shaun-mahood I think that makes sense too. I find it the practice of writing accessor functions for the db somewhat bothersome. Just another set of functions to manage.Oh right, of course.
Just remove the extra (fn [] ...)
and move that @
right in front of (rf/subscribe ...)
and you're all set.
(defn search-form
[]
(let [needed-val @(rf/subscribe [:my-sub])]
[:div#search-form
[:button {:on-click #(rf/dispatch [:my-event needed-val])}]]))
Like that, huh?
I see, the subscription gets cached, so you don’t need a Form-2 reagent component.
Hmm, but I don’t see why I can’t deref the subscription within the #(rf/dispatch ….)
anonymous function.Since you're referencing that, if you have a chance to read the docs I wrote for that issues and comment on anything that needs more work that would be helpful