mst-async-storage
mst-async-storage copied to clipboard
Load overrides defaultState
Thanks for the great work. I have an issue with the package in that it overrides defaultState that I specify in create(). I tried except and only, but both don't seem to work.
export const RootStore = types.model('RootStore', {
products: types.array(Product),
boxes: types.array(Box),
user: types.maybeNull(User)
}).extend(withAsyncStorage({
key: "MyRootStore",
only: [
'user'
]
}))
const defaultState = {
products: [
{
id: 1,
name: 'Red Apple',
price: 50,
image: "https://supermart.ae/modules/advancedslider/images/2f49fa65f2818000aed5efe61aaa1418.jpg",
numberInCart: 1
}
],
boxes: [
{
id: 1,
name: "Ramadan Box"
},
]
} // This is the default state
const MSTStore = RootStore.create(defaultState); // It will overwrite boxes and products
I see. Hmm.
I use applySnapshot which will overwrite the contents of the store.
They don't seem to have the concept of mergeSnapshot which is what you're expecting.
Do they have something like that, do you know?
They have an option to applyPatch. I tried it like this:
await MSTStore.load();
applyPatch(MSTStore, {
op: 'replace', // It could be replace, add, or remove
path: 'products', // name of the node, perhaps fetch it from (only-except) fields?
value: defaultState.products // The data
} // Could be an array of patches
);
And it works great, you can even array the patches. I fixed it from my state by patching in the defaultState. But I assume you could use applyPatch instead of applySnapshot.
Nice one. I'll take a closer look and see if we can work this in somehow. Perhaps alternate modes or something. Thanks!