polemius / recoil-persist

Package for recoil state manager to persist and rehydrate store
https://polemius.dev/recoil-persist/
MIT License
348 stars 40 forks source link

support custom serialization/deserialization. fixes #75 #78

Closed jedelson-pagerduty closed 1 year ago

jedelson-pagerduty commented 1 year ago

The solution described in https://github.com/polemius/recoil-persist/issues/75#issuecomment-1474769954 doesn't work (at least for my use case) because setItem accepts a string whereas I (and I suspect @tpict) need to control how stringify/parse actually work.

polemius commented 1 year ago

Could you please update documentation as well?

jedelson-pagerduty commented 1 year ago

Could you please update documentation as well?

Done

jedelson-pagerduty commented 1 year ago

Please add mention of converter to Usage section

I assume in the Usage section it makes sense to show the default but please let me know if you think something else should go there.

polemius commented 1 year ago

I assume in the Usage section it makes sense to show the default but please let me know if you think something else should go there.

I thought maybe we could show in Usage section all options like:

import { recoilPersist } from 'recoil-persist'

const { persistAtom } = recoilPersist({
  key: 'recoil-persist', // this key is using to store data in local storage
  storage: localStorage, // configurate which storage will be used to store the data
  converter: JSON, // .... 
})
jedelson-pagerduty commented 1 year ago

Yeah, what I put there is

import { recoilPersist } from 'recoil-persist'
const { persistAtom } = recoilPersist({
  key: 'recoil-persist', // this key is using to store data in local storage
  storage: localStorage, // configurate which storage will be used to store the data
  converter: { // configurate how values will be serialized/deserialized in storage
    parse: (value) => JSON.parse,
    stringify: (value) =>  JSON.stringify
  }
})

Although converter: JSON would be semantically the same thing, I think it's useful to show both functions. WDYT?

polemius commented 1 year ago
converter: { // configurate how values will be serialized/deserialized in storage
    parse: (value) => JSON.parse(value),
    stringify: (value) =>  JSON.stringify(value)
}

you need to pass values to function

polemius commented 1 year ago

Good job! Thank you!

polemius commented 1 year ago

Released in 5.0.0 🚀