question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

persist API: store initial value

See original GitHub issue

Hey 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:closed
  • Created 2 years ago
  • Reactions:4
  • Comments:13 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
TheEhsanSarsharcommented, May 23, 2021

Ok, I got it.

  • async await
  • DX
2reactions
dai-shicommented, May 20, 2021

@dai-shi we can have { presistedFields: [ "fieldname" ] } inside persistoptions object. if you will I can work on this.

I don’t think it’s that easy. We also need to take care of AsyncStorage.

(beware, however, that this currently happens asynchronously: #346).

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found