-
Notifications
You must be signed in to change notification settings - Fork 35
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
persisted()
returns undefined
first time it's accessed, even when initial
is set
#81
Comments
persisted()
returns undefined
first time it's accessed, even when initialValue
is setpersisted()
returns undefined
first time it's accessed, even when initial
is set
I have the same issue with persistent, it starts with undefined even though it has initial value. Maybe, one way to fix it is passing the initial value to the thisStore writable. This way subscribe function will return the correct initial value. let initialValue
if (isLoadable(initial)) {
initialValue = await initial.load();
} else {
initialValue = initial
}
const thisStore = writable<T>(initialValue, (set) => {
initialSync = synchronize(set);
}); The if conditional could go into a dedicate function, since it is the same code inside of syncronize. Any way, this is just a possible solution. 😉 |
This is not an acceptable solution in the case that the initial value NEEDS to be equal to the value that the user has stored in localStorage. |
Hi! I'm having the same problem. Is there any way to fix it? |
But it's better than the current state. |
Hunn, how about this then: let initialValue
const storageKey = await getKey();
const stored = getStorageItem(storageKey);
if (stored) {
initialValue = stored
} else {
if (isLoadable(initial)) {
initialValue = await initial.load();
} else {
initialValue = initial
}
}
const thisStore = writable<T>(initialValue, (set) => {
initialSync = synchronize(set);
}); I can make a PR with this, if the maintainers agree with. |
I've solved my case with such a wrapper
It doesn't wait to load the persisted value and always returns the initial value on start. |
I have the following setup
which I then use in my component
The problem is, that the first time the store is initialized and saved to the local storage with it's default value,
$lastLocation
is undefined. I need to reload the store/page.This line is the culprit https://github.com/square/svelte-store/blob/main/src/persisted/index.ts#L140
The initial value should be passed, NOT undefined
The text was updated successfully, but these errors were encountered: