Migrate legacy values to Zustand #3261
Unanswered
davor-bauk-sh
asked this question in
Ideas
Replies: 1 comment
-
|
Hi, thanks for reporting. I wonder if this patch works for your case: diff --git a/src/middleware/persist.ts b/src/middleware/persist.ts
index 5a2dfe5..8219646 100644
--- a/src/middleware/persist.ts
+++ b/src/middleware/persist.ts
@@ -101,7 +101,7 @@ export interface PersistOptions<
*/
migrate?: (
persistedState: unknown,
- version: number,
+ version: number | undefined,
) => PersistedState | Promise<PersistedState>
/**
* A function to perform custom hydration merges when combining the stored state with the current one.
@@ -266,7 +266,7 @@ const persistImpl: PersistImpl = (config, baseOptions) => (set, get, api) => {
.then((deserializedStorageValue) => {
if (deserializedStorageValue) {
if (
- typeof deserializedStorageValue.version === 'number' &&
+ typeof options.version === 'number' &&
deserializedStorageValue.version !== options.version
) {
if (options.migrate) { |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
It would be nice if the
migratehandler in thepersistmiddleware allowed migrating legacy values into a new Zustand store.Let's say my app state depends on the following two values in AsyncStorage:
foo: stringbar: string... and I set up a new store:
I assumed that I could use the
migratehandler to pull in legacy values into the store. In fact, this was suggested by both Claude and GPT.However, the
migratehandler only gets called if there is an existing stored state with a version different than the current one.Also, the state doesn't seems to be saved to storage until it is mutated.
It would be cool if we could do something like this:
I'm able to make it work with this patch:
If there is currently a better way to do this, I'd love to know about it!
Beta Was this translation helpful? Give feedback.
All reactions