Fork me on GitHub
#re-frame
<
2022-09-09
>
popeye09:09:16

If I define coeffect as below, How to consume 10 in coeffect? Below cofx did not worked for me

(inject-cofx :random-int 10)



(rf/reg-cofx
 :random-int
  (fn [coeffects [_ int-val]]
    (println "---E---"int-val)
  ))

p-himik09:09:58

Have you read the docstring of inject-cofx?

p-himik09:09:45

Ah, hold on - I misread the question.

p-himik09:09:22

The value is passed like this:

(update context :coeffects handler value)
where handler is that fn. So the second argument to your fn is actually just int-val.

popeye09:09:09

(update context :coeffects handler value) do we have to call in reg-event-fx ?

p-himik10:09:32

No, I mean that that's the line that re-frame uses when it injects the cofx.

p-himik10:09:44

All you have to do is to replace [_ int-val] in your code above with int-val.

popeye11:09:46

ahh got it, Thanks

popeye13:09:26

I have one more question! if we want to pass any values from dispatcher, how can we get it in coeffect ?

p-himik13:09:49

Uhmm, you can't?.. Unless I completely misunderstand your question, that would be akin to calling a function to get a value of an argument for that very function. That would require some time travel.

popeye13:09:48

yeah, I doubted this, Just wanted confirmation 🙂

popeye13:09:59

Thank you for your response

p-himik14:09:40

Ah, once again, after letting my brain think for a few seconds longer, I realize that I probably did misunderstand your question. :) If by "dispatcher" you mean the caller of dispatch, you should be able to extract the full event vector from the coeffects map with an :event key.

popeye14:09:13

any example ?

p-himik14:09:14

(rf/dispatch [:my-event :witness-me])

(rf/reg-cofx :my-cofx
  (fn [coeffects]
    (let [[event-id first-arg] (:event coeffects)]
      (assert (= first-arg :witness-me))
      coeffects)))

(rf/reg-event-fx :my-event
  [(rf/inject-cofx :my-cofx)]
  (fn [...] ...))

popeye14:09:17

what is :event key , is that inbuilt in reframe ?

p-himik14:09:20

That's how re-frame threads the event vector itself throughout its machinery, and it makes it available for you.

popeye14:09:12

did I miss this concept from reframe documentation ?

p-himik14:09:00

In the code, at the very least the docstring of reg-event-ctx mentions (:event coeffects). And in the documentation, http://day8.github.io/re-frame/Interceptors/#what-is-context

popeye14:09:22

ohh I see!, I can able to reproduce in my system, Great learning today 🙂 Thank you

👍 1
erwinrooijakkers14:09:49

(rf/reg-cofx
 :random-int
  (fn [cofx int-val]
    (println "---E---"int-val)
    (assoc cofx :random-int int-val))

(rf/reg-event-fx :my-event
  [(rf/inject-cofx :random-int 10)]
  (fn [{:keys [random-int db] :as cofx} _])
    {:db (assoc db random-int)}))
now you have a coeffect “random-int” (with the value of 10) that is injected into the coeffects map for the event :my-event

Aspirational11:09:58

will this component be reactive as the first deref is made in the let bindings block? (defn recipe-component [] (fn [] (let [menu-state (rf/subscribe [:adminPanel :subMenuState]) recipe (rf/subscribe [:recipes (:itemId @menu-state)])] [:div @recipe])))

p-himik11:09:29

It will be. Doesn't matter whether it's in a let or no. What matter is that it's done within the view function and outside of any JS event handler function or something like that. BTW that nested (fn [] ...) creates a form-2 component. You don't need it here, you can just go with a form-1 by raising the function's body.

2
🙌 1