-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
persist API: store initial value #366
Comments
Your case is, basically, calling the "guuid()" function per re-render time, which leads into the changing of your "id" state. I don't think that a problem with the library, it is more likely that the way to approach your problem haven't correct yet. |
As @Mangor1no mentioned, you can approach the problem another way. For instance, you could set the ID in the store from a const useStore = create(persist((() => ({
id: undefined,
setId: (id) => set({ id })
...
})));
function App() {
const id = useStore(state => state.id);
const setId = useStore(state => state.setId);
useEffect(() => {
setId(guuid());
}, []); // run effect on mount
if (id === undefined) {
return null; // app not ready
}
return <>...</> // app ready
} Alternatively, you could set the new ID to |
thank you for your replies, I'm aware I can get around this problem by using the 'set' fn. I just find it unconvenient that the store value is persisted only after explicit interaction with it. |
@dai-shi we can have |
I don't think it's that easy. We also need to take care of AsyncStorage.
#346 (comment) |
I was about to open a new issue, but then I spotted this thread and I think I might be running into the same problem. This example was adapted from the docs: https://codesandbox.io/s/pedantic-smoke-96udc. The state is random on every load until clicking the button, after which it's persisted correctly. I took a quick stab based on @axelboc's example: https://codesandbox.io/s/dank-dream-qojuh, but it actually doesn't work at all. The state is random every time, even after clicking the button. For context, my goal is to store a session token when a user authenticates, and then store a local cache of their data (along with metadata such as the last time it was refreshed). I'm not sure if there's a better way of approaching this. Any ideas? |
as it seems there is some inconsistency in persisting data. in this repo the data will only store in localstorage when you click on update button. |
@dai-shi the issue is that persist middleware only persists data when there is a set call. or this |
OK, so, it's another async issue on set. In addition to #366 (comment), we'd need to check promise on set. Hope you get the point. It's rather a big refactoring. |
Ok, @dai-shi I will look for a solution for |
Good. That's the first step. What you were trying to solve is the second step, which could be done after that, if that's really desired. |
Ok, I got it.
|
Closing this as workarounds are provided and the discussion is a little too broad. |
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:
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.
The text was updated successfully, but these errors were encountered: