persist API: store initial value
See original GitHub issueHey guys, first of all, I really like the library! I’d like to extend the PersistOptions API to fit my needs. I’ll briefly introduce my use case: I want to set the unique id to the client device, smth like this:
create(persist((() => ({
id: guuid(),
...
}), { name: localStorageKey }));
Every time I refresh the page, the id changes. I’d love to have that state value saved to the localStorage at the time of a new store creation. I’m aware I can declare extra action that sets the id explicitly, but I think an extra property to PersistOptions, smth like { storeInitialValue: boolean }
(store only when localStorage.name does not exists) would be a much cleaner solution.
And of course, maybe I’m missing something, but I’d love to hear your feedback on this.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:4
- Comments:13 (9 by maintainers)
Top Results From Across the Web
How to Persist Redux State to an API Part 1 - YouTube
Start learning with the Covalence community at https://covalence.ioGitHub Repo for Starting ...
Read more >How to persist variable values - Postman
Persist all current values; Persist only some of the current values; Programatically update initial values from pre-request or test scripts ...
Read more >React Context API - persist data on page refresh
One of the things you can do is to store the value in the browser's Cookie or any storage available to you, and...
Read more >Persist middleware - Zustand Documentation
By default, if the version in the storage does not match the version in the code, the stored value won't be used. You...
Read more >How to Save State to LocalStorage & Persist on Refresh with ...
When storing something in state, we're essentially saying that we have a special value that we want to stick around that we can...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Ok, I got it.
I don’t think it’s that easy. We also need to take care of AsyncStorage.
https://github.com/pmndrs/zustand/issues/346#issuecomment-810342287 We should fix this, and then localStorage users can store initial values before calling
create()
. You want to work on this? hint: https://github.com/pmndrs/jotai/blob/8e1366af11251e3873b9fddde5ffd011a469d95c/src/utils/atomWithStorage.ts#L39-L42