Fork me on GitHub
#re-frame
<
2022-04-07
>
Richard Bowen16:04:12

How do I call a namespaced effect (`::effect`) from an event? {:effect "Something"} doesn't work and produces a re-frame: no handler registered for effect: error.

p-himik16:04:22

Just use the correct keyword? If your effect is in ns a.b and your event handler is in ns b.c, then use :a.b/effect. Or add [a.b :as b] to your :require and use ::b/effect.

Richard Bowen16:04:08

Indeed, thanks.

👍 1
Richard Bowen18:04:17

How does one call a reg-sub from an event using it as a parameter to an effect? Eg. I have an event, ::copy-path , which returns the result of an effect ::b/copy , the parameter for ::b/copy should be string returned by a reg-sub ::path .

p-himik18:04:45

An easy and correct way is to deref the sub in the relevant view and pass the value itself as a part of the event vector. There's an FAQ entry for that.

Ryan19:04:13

In general do subs need to be put at the top of the view, or can sub-views have subscriptions as well? Troubleshooting some subscription problems and trying to rule stuff out.

p-himik19:04:11

What do you mean by "sub-view"?

Ryan19:04:55

I thought maybe my problems were related to subscriptions in nested components, but that can't be it.. I think its related to de-refing subs in let statements instead of function bodies.. tracking down a very silent subscription-not-running problem

p-himik19:04:48

So far, what you describe doesn't make much sense. If you can't figure it out, create a minimal reproducible example and I'll take a look.

p-himik19:04:43

As a guess, if you have code like this then the component will not be re-rendered on the sub change:

(defn component []
  (let [v @(rf/subscribe [...])]
    (fn []
      [:div v])))

Ryan19:04:31

Your guess is very very good 🙂

Ryan20:04:21

What about

(defn component []
  (let [v (rf/subscribe [...]
       [x (+ 1 @v)]
    (fn []
      [:div x])))

p-himik20:04:26

Same thing. Why do you even use a form-2 component? Just make it a form-1 one and that's it.

Ryan20:04:00

An excellent point, I should figure out when to use form-2

p-himik20:04:58

When you have state that should persist between re-renders of the component or when you have some setup code that should be run before the very first mount.

p-himik20:04:20

And even then, you often can use reagent.core/with-let instead - sometimes it makes things shorter and more natural.

🙏 1
Ryan20:04:39

Thanks for the clarification

👍 1
Richard Bowen19:04:51

Got an event here:

(reg-event-fx
 ::copy-something
 (fn [{:keys [db]}]
   {::effect/copy (:something db)}))
However, this doesn't fetch :something from db What is the correct way to pull data from db?

p-himik20:04:38

That's how you do it. Inspect whether db is in the state that you expect it to be in.

Richard Bowen20:04:45

How do I inspect db? I've been using (js/console.log) but that doesn't show any of the data in the db.

p-himik20:04:23

Two ways - either (js/console.log db) with cljs-devtools installed or re-frame-10x. If the former doesn't show any data (i.e. it shows null), then you broke your DB somewhere before that event.

p-himik20:04:48

A good thing to have is a global interceptor that checks your DB against the spec after every single event.

Richard Bowen20:04:21

The console log value I get is:

{meta: null, cnt: 42, root: {…}, has_nil_QMARK_: false, nil_val: null, …}
cljs$lang$protocol_mask$partition0$: 16123663
cljs$lang$protocol_mask$partition1$: 139268
cnt: 42
has_nil_QMARK_: false
meta: null
nil_val: null
root: {edit: null, cnt: 23, arr: Array(32), cljs$lang$protocol_mask$partition1$: 131072, cljs$lang$protocol_mask$partition0$: 0}
__hash: null
[[Prototype]]: Object

p-himik20:04:10

Right, you should install cljs-devtools - it'll make your life much easier. What you see above is a JS representation of a CLJS object.

👍 1
Richard Bowen22:04:25

Any other way to inspect the db? I'll have to discuss adding cljs-devtools . re-frisk is already available to the project.

Richard Bowen23:04:56

Got it sorted.

👍 1