How to deal with changes in the structure of persisted(...)
#310
-
Hello, I'm building a website and I'm using this repo to store the user preferences (for example about whether or not to show a popup, notifications and so on). The structure is the following: export const preferences = persisted('preferences', {
/**Wether to show the welcome message when the page is first loaded */
showWelcomeAtStart: true,
notifications: {
issuesOpened: false,
issuesCancelledTimes: 0,
},
tutorial: {
open: false,
}
}) Let's say that a version of the website with these Example usage of the preferences: let viewport = .... // The html element containing the scrollable text
viewport?.scrollTo({
left: 0,
top: prefs?.scrollPosition,
behavior: 'smooth'
});
// ...
viewport?.addEventListener('scroll', () => {
preferences.update((x) => ({
...x,
tutorial: { ...x.tutorial, scrollPosition: viewport?.scrollTop }
}));
}); |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Hi @gamberoillecito, I believe what you are trying to do is migrate the state of the store. There are several techniques you can use:
|
Beta Was this translation helpful? Give feedback.
Hi @gamberoillecito,
I believe what you are trying to do is migrate the state of the store.
There are several techniques you can use:
beforeRead
to detect if the data stored is an old version and migrate to new version.version
field in the store and check that when instantiating the store.preferences.v1
and add some migration logic.undefined