-
-
Notifications
You must be signed in to change notification settings - Fork 643
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Improve docs for atomWithStorage and persistence #805
Comments
@ryandi-tiket Let's use this issue. Would you explain your problem with |
Mismatch server/client HTML. Plus we need to sanitize the localStorage value before it sets. |
@ryandi-tiket Thanks! I see the issue is the mismatch warning. |
Changing const anAtom = atomWithStorage('ls_key', []) to const anAtom = atomWithStorage('ls_key', [], {
...createJSONStorage(() => localStorage),
delayInit: true,
}) seems to have the same render behavior/timing as what I had going with const anAtom = atom([])
anAtom.onMount = (setValue) => {
const possiblyInvalid = JSON.parse(localStorage.getItem('ls_key) ?? '[]')
const validated = myValidator(possiblyInvalid)
setValue(validated)
localStorage.setItem('ls_key', JSON.stringify(validated))
} I don’t think there exists an API for this in |
Good to hear that and thanks for the last mile. |
By unpredictable I mean I’m ceding control and can’t be sure when/how often the validator will be called. Does jotai re-getItem from time to time? Will it do so for every instance of useAtom? If it’s computationally expensive, I’d only want it to run once on mount. |
It runs just once: jotai/src/utils/atomWithStorage.ts Line 89 in 7c3f51c
As you are already familiar with |
Looks good. I think it just needs better docs, and perhaps slightly better ergonomics in this line: As for the validators, I’m thinking provide options that could be passed as 2nd argument to createJSONStorage, instead of another function const anAtom = atomWithStorage('ls_key', [], {
...createJSONStorage(() => localStorage, { onGetItem: (value) => myValidator(value), onSetItem: (value) => myValidator(value) }),
delayInit: true,
}) |
If we add options to createJSONStorage, we can add createJSONStorage(() => localStorage, { delayInit: true }) I'm more than happy if you can help us on docs. |
|
No, no. |
@dai-shi There are a few linked subjects already:
I'm trying to organize all that and they're already a few discussions/PR/issues about it. Maybe we could close this one? |
@TwistedMinda Can you check all bullet points in OP are covered? |
@dai-shi yep to me it's kinda all good. Except the type guards which I don't know what are these :p |
This is to create a custom storage and make it type safe. |
Looks like sessionStorage is missing? #1220 |
@dai-shi Indeed, i'll take care of this |
I am using typescript and this is the error I get |
#394 (comment) @jrobber
atomWithStorage
atomWithStorage
docs should explain how to use non default storage like sessionStorage and AsyncStorageatomWithStorage
docs should explain how to use non JSON.parse/stringify serializeratomWithStorage
docs should explain how to type atomsdelayInit
atomWithStorage trouble with AsyncStorage #780 (comment)The text was updated successfully, but these errors were encountered: