Making an atom of a subscription requires derefing twice. Problem only occurs when using defonce

52 views Asked by At

When I use def to save an atom, it works as expected. But when I use defonce, I have to deref twice: @@my-state. I want to use defonce because I want state preserved during reloads.

This works as expected

(def my-state (reagent/atom (re-frame/subscribe [::subs/photos])))

This requires two derefs to access the values

(defonce my-state (reagent/atom (re-frame/subscribe [::subs/photos])))

Subscription code

(re-frame/reg-sub
  ::photos
  (fn [db [_]]
    (:photos db)))
1

There are 1 answers

0
Eugene Pakhomov On

I want to use defonce because I want state preserved during reloads.

Then just don't use Reagent's atoms, use re-frame's subscriptions instead. They derive their values from re-frame's app-db which is itself defined with defonce.