Fork me on GitHub
#re-frame
<
2017-10-01
>
r620320:10:25

Is it fine to update state inside an event handler based on another state value or would that be "impure"? Basically, I want to replace a value with another one from the app state. Here's an example:

(re-frame/reg-event-db
 :save-client
 (fn [db [_ client]]
   (let [id (key client)]
     (assoc-in db [:clients id] (:editing-client db))))) ;; I figure out the correct way to update. This is more of an "architectural question". 

danielcompton20:10:18

@robin896 that's fine, you get given an entire app-db as input state as well as your params

danielcompton20:10:57

As you mentioned, it does mean that the event on it's own isn't quite enough to understand what will happen

danielcompton20:10:21

but passing all the state you would need into the view, then through the event, then back into the event handler will get old very fast

danielcompton20:10:23

My rule of thumb is to go with what feels more natural for the data. If the data is being edited/displayed in the UI, then passing it through as an event is ok, but if it's more of an implicit state of your app then referencing it from app-db is ok

r620320:10:24

@danielcompton Ok, thank you. That's exactly what I thought. 🙂