Fork me on GitHub
#re-frame
<
2021-01-08
>
emccue00:01:56

@ps try doing this instead

emccue00:01:25

(require '[re-frame.core :as rf])

(rf/reg-fx 
  ::get-from-async-storage
  (fn [{:keys [key when-retrieved]}]
    (-> (. AsyncStorage getItem key)
        (.then 
          (fn [value]
            (rf/dispatch (when-retrieved value)))))))
     

emccue00:01:52

Make an FX handler - this is the low level construct for defining how a side effect is performed

emccue00:01:11

and give it a function to call when it is done that will return an event to dispatch with the new value

emccue00:01:25

so then in your event handler

emccue00:01:24

(rf/reg-event-fx
  :register-db
  (fn [db _]
    {:db db
     :fx [[::get-from-async-storage {:key "_id"
                                     :when-retrieved #(vector :got-value %)}]]}))

(rf/reg-event-db
  :got-value
  (fn [db [_ value]]
    (assoc db :value value)))

emccue00:01:44

so you need two events

emccue00:01:03

but there are (at least) two things happening that are not part of the model

emccue01:01:35

try to have your fx handlers be few in number

emccue01:01:43

and just write helpers on top of them