This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2016-02-19
Channels
- # beginners (25)
- # boot (143)
- # braid-chat (9)
- # cider (18)
- # cljs-dev (88)
- # cljsrn (1)
- # clojure (91)
- # clojure-austin (2)
- # clojure-berlin (3)
- # clojure-japan (26)
- # clojure-russia (148)
- # clojurebridge (1)
- # clojured (29)
- # clojurescript (105)
- # cursive (7)
- # data-science (4)
- # datomic (15)
- # devcards (4)
- # emacs (8)
- # euroclojure (2)
- # events (1)
- # gsoc (27)
- # hoplon (3)
- # immutant (3)
- # ldnclj (3)
- # lein-figwheel (9)
- # leiningen (2)
- # luminus (1)
- # off-topic (5)
- # om (176)
- # onyx (136)
- # parinfer (16)
- # proton (13)
- # re-frame (33)
- # reagent (34)
- # spacemacs (1)
- # yada (127)
I have a function which should update a handler, and then immediately display the corresponding subscription. However, the ratom isn't updated until the entire function has run.
(defn poll-button [stateclass emailclass]
(fn []
[:input {:type "button"
:class "btn btn-info"
:value "Vote"
:on-click #(if (email-filled?)
(do
(dispatch [:enter-contact "Joy to the fishes in the deep blue sea."])
(js/alert @(subscribe [:contact]))
;; close Modal window
(reagent-modals/close-modal!))
(reset! emailclass "has-error"))}]))
I thought the do
block might be to blame, so I pulled it into it own function :thinking_face: but no dice
The second time I push my button, :contact
has updated correctly. Any ideas greatly appreciated!
Basically, I'd like the side-effect of calling dispatch
(an updated Big Atom) to be reflected immediately. Which is what I thought do
was for.
dispatch-sync
will do what you want, but what you are trying to do is something of an anti-pattern.
And do
does not do what you think it does (not often you get to say that line).
That's very yoda of you 😉
Thanks for the tip about dispatch-sync
. And when you say it's an anti-pattern, is that because I should probably just send along my data directly, instead of through a handler-subscription duo?
(I have an already-written function which subscribes to :contact
, and which I will call instead of js/alert
, so here I am lethargically using :enter-contact
to send data to that pre-existing function)
This seems relevant to my questions: https://github.com/Day8/re-frame/wiki/FAQ#3-why-cant-i-call-dispatch-sync-in-an-event-handler
Not so much yoda, more Inigo Montoya
Using dispatch-sync
in a Component feels very imperative. It is like the Component is trying to change the world. In re-frame Components do not try to change the world. They simply render the current state (including dispatches). They are a simple function of the state (which produces hiccup).
It is event handlers which change the world
So, I'd move whatever world changing (state changing) thing you are doing into an event handler
The handler for that button click (everything in the do
should live in an event handler). The on-click handler should just be (dispatch [:something-clicked]
)
The associated event handler might perhaps alter the :modal-showing?
piece of state. It might even put in place data for that modal to edit.
Somewhere among your Components there is one which subscribes to the :modal-showing?
and when it is true, it shows the modal.
[:div (if @modal-showing-subscription [my-modal XXX])]
....thinking....
Yes, I see what you're saying. Here, I'm just sending a form to the back-end, and relying on a function with a subscription to do the sending. That function made sense before, but here jury-rigging a Component to send stuff is silly.
Thanks for clarifying, I'm keeping your thoughts for future reference as well 😎
Glad to help. Good luck. Be sure to read the re-frame Wiki.
(yeah, I know, more reading!)
lol it's surprising how many things I keep re-discovering in the Wiki
It is a many-layered onion, as is the original readme.
I've now built two (related) production apps with re-frame. I just started hacking stuff together with reagent and eventually found myself with state scattered here and there. Re-frame provided a graceful and even elegant solution and way of thinking about state.
So I've appreciated the readme many times (along with the many jokes, ecclesiastical and otherwise ;-))
I also find myself using more re-com components
Anyway, thanks for both
Excellent! I'd love for re-com
to get a bit more use out there.
I must admit we are a bit slow on new releases, but there's a lot of hard-fought-for knowledge in there
Well, I keep multiplying components needlessly - a sane way to abstract over components, and allow flexibility/customization, is helpful.
So over time I get pushed toward re-com
as well 👍