Fork me on GitHub
#re-frame
<
2020-03-04
>
gekkostate18:03:57

Hi all, quick question. What is the impact on the store if reg-event-fx returns a nil value? I have a reg-event-fx which uses an if-let. I noticed with reg-event-db that my db value was "niled" if there was an if which returned nil.

chrisulloa18:03:43

I think you’ll want to return the unchanged db in your reg-event-fx {:db db} rather than having it return nil.

chrisulloa18:03:17

same with reg-event-db, you’ll want to return the existing db rather than a nil value in case of a no-op

chrisulloa18:03:54

forgot if you can omit the db key from reg-event-fx, might want to try that out

dpsutton18:03:24

from my experience you can return nil

4
chrisulloa18:03:40

yeah just testing it out, reg-event-fx doesn’t need {:db db}, you can just return nil and no side effects get called.

gekkostate18:03:49

Ok, great. Yea, I noticed that as well and wanted to double check 😄 So, for reg-event-db we have to return {:db db} and reg-event-fx we can return nil with no issues. Is this in the docs somewhere?

chrisulloa18:03:45

i think the way to look at it is that reg-event-fx takes a map of coeffects to perform

chrisulloa18:03:02

reg-event-db’s job is only to perform the :db coeffect

chrisulloa18:03:07

which swaps the existing db with the one provided

gekkostate20:03:05

Ahhh ok, that makes sense. This is why we have to provide some value to swap.

chrisulloa18:03:17

To make this common case easy to program, there's a specific registration function, called reg-event-db, which delivers ONLY the coeffect db to the event handler (and event of course).

escherize20:03:36

I found the way reg-fx is used to setup the :db effect handler was useful to show how reg-fx works: https://github.com/day8/re-frame/blob/master/src/re_frame/fx.cljc#L160