Fork me on GitHub

Hey all. I have maybe a silly question. I am trying to make use of local storage. I am able to set a key/val and I would like to access it via coeffects:

 (fn [cofx k]
   (assoc-in cofx [:local-storage k] (get-item k))))
I cannot seem to find the :local-storage key inside of my coeffects map when inspecting events from reg-event-db. Do I have to include this somewhere? Currently it is just registered and not included anywhere


Yes, otherwise reg-cofx is never called and re-frame doesn't know about it. You should've received a warning in the JS console mentioning it.


I guess I'm not sure how to include it. I saw there is a way to inject into co-fx, however that seems to be different to what I want t odo


Ah, just include the namespace somewhere, that's it!


Oh? So the namespace is included in the larger events ns. I guess I'm wondering how to get that data I saved in local storage out? A subscription?


Ah, if it's already included then that ::get should be available. Just make sure that you're using the right namespace, because ::get in one ns is different from ::get in another one.


If you need the data in an event, then a cofx is a right tool - not a sub. Especially since subs wouldn't know about LocalStorage changes, unless you decided to duplicate all the data in your app-db or do something very clever with reg-sub-raw.


Ah, well damn. It seems like maybe a cofx is not what I want. But for my knowledge. I am still not seeing it inside of the cofx map: Do I have to do something like this:

 (fn [cofx _]
   {::localstorage/set! [:meow "hithere"]
    ::localstorage/get :meow}))


No idea, there's too little information for me to give you a useful answer. Have you tried looking at the implementation of the things that already exist out there? Like and for example.


Oh, so I do use inject. Thanks


Is there a reason why you wouldn't suggest using a cofx to modify the app db upon initialization of the page?


(Thanks for your help here, btw)


Sure thing. But I'm still not sure what exactly we're talking about. Why would you want to use a cofx and not the event handler itself to modify app-db?


Because I want the data to persist app closes


So I am storing the data inside of localstorage


I guess I could use an event handler to get the data from local storage


Do you think that is a better approach?


When the app loads, dispatch an event. In its handler, read the local storage and populate app-db with its contents. On each app-db change, sync the relevant values to the local storage - should be easy with a global interceptor.


That makes sense.Thanks

👍 3