Fork me on GitHub
#re-frame
<
2021-08-17
>
PB19:08:45

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:

(rf/reg-cofx
 ::get
 (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

p-himik19:08:59

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.

PB19:08:54

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

p-himik19:08:21

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

PB19:08:48

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?

p-himik19:08:06

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.

p-himik19:08:14

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.

PB19:08:34

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:

(rf/reg-event-fx
 ::dothething
 (fn [cofx _]
   {::localstorage/set! [:meow "hithere"]
    ::localstorage/get :meow}))

p-himik19:08:07

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 https://github.com/akiroz/re-frame-storage and https://github.com/deg/re-frame-storage-fx for example.

PB19:08:37

Oh, so I do use inject. Thanks

PB19:08:31

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

PB19:08:49

(Thanks for your help here, btw)

p-himik19:08:13

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?

PB19:08:16

Because I want the data to persist app closes

PB19:08:27

So I am storing the data inside of localstorage

PB19:08:57

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

PB19:08:07

Do you think that is a better approach?

p-himik19:08:29

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.

PB19:08:35

That makes sense.Thanks

👍 3