|
| 1 | +<!-- leave this H1 here. It stops mkdocs putting in a Title at the top. |
| 2 | + It needs to be at the top of the file otherwise it breaks the |
| 3 | + table of contents on the right hand side. --> |
| 4 | +# |
| 5 | + |
| 6 | +## Question |
| 7 | + |
| 8 | +With `reg-event-fx`, why can't my handler function just update the first argument (i.e. the `cofx` map) and pass it on? |
| 9 | + |
| 10 | +When I try, I get a warning, such as: `no handler registered for effect: :event. Ignoring.` |
| 11 | + |
| 12 | +## Answer |
| 13 | + |
| 14 | +Effects simply aren't coeffects. |
| 15 | + |
| 16 | +To fix this warning, just declare a new map of effects. For instance: |
| 17 | + |
| 18 | +```clj |
| 19 | +(reg-event-fx ::cow-clicked |
| 20 | + (fn [{:keys [db] :as cofx} _] |
| 21 | + {:db (update db :clicks inc)})) |
| 22 | +``` |
| 23 | + |
| 24 | +## Context |
| 25 | + |
| 26 | +This seems like it would work, if you're used to using `reg-event-db` this way. |
| 27 | +Such a `db` handler behaves like a reducing function: |
| 28 | + |
| 29 | +```clj |
| 30 | +(reg-event-db ::cow-clicked-db |
| 31 | + (fn [db _] (update db :clicks inc))) |
| 32 | +``` |
| 33 | + |
| 34 | +Doing a similar thing to a coeffect map causes the warning, however, not to mention possible unintended effects: |
| 35 | + |
| 36 | +```clj |
| 37 | +(reg-event-fx ::cow-clicked-bad |
| 38 | + (fn [cofx _] (update-in cofx [:db :clicks] inc))) |
| 39 | +``` |
| 40 | + |
| 41 | +With `reg-event-fx`, you receive a map of coeffect values, and you're expected to return a map of effect values. |
| 42 | +It's a coincidence that `:db` is the name of both an effect and a coeffect. |
| 43 | +But re-frame adds other coeffects, such as `:event`, which it does *not* consider effects. |
| 44 | + |
| 45 | +So your handler ends up doing `(update-in {:db {:clicks 0} :event [::cow-clicked-bad] :your-other-coeffect 25} ...)`. |
| 46 | + |
| 47 | +Any coeffects you declare cause this problem, too. You might have a coeffect, like `:db`, which shares its name with an effect. But that's a deliberate design decision, not a default. |
0 commit comments