mobx-state-tree: Error updating to MST 1.1.0 from 1.0.1
Hi guys, we are having some issue with frozen types after updating to the last version. In our rootStore (we have one for redux dev tools), we are having this issue, but the most important thing here, is that this just happen when we build the project with CRA and upload to the server.
RootStore.js
const RootStore = types
.model('RootStore', {
languageStore: types.optional(LanguageStore, getSnapshot(languageStore)),
folderStore: types.optional(FolderStore, getSnapshot(folderStore)),
authStore: types.optional(AuthStore, getSnapshot(authStore)),
templateStore: types.optional(TemplateStore, getSnapshot(templateStore)),
geniallyStore: types.optional(GeniallyStore, getSnapshot(geniallyStore)),
myBrandStore: types.optional(MyBrandStore, getSnapshot(myBrandStore)),
inspirationStore: types.optional(
InspirationStore,
getSnapshot(inspirationStore)
),
invoiceStore: types.optional(InvoiceStore, getSnapshot(invoiceStore)),
modalStore: types.optional(ModalStore, getSnapshot(modalStore))
})
.actions(self => ({
afterCreate() {
self.authStore.fetchUserData();
}
}));
const rootStore = RootStore.create(); // THE ERROR IS HERE WITH TEMPLATE STORE (SEE BELOW)
export default rootStore;
The error is in this store, templateStore, we are using frozen on it like this:
const TemplateStore = types
.model('TemplateStore', {
templates: types.array(Template),
status: types.enumeration('State', ['loading', 'loaded', 'error']),
search: types.optional(types.string, ''),
templateType: types.maybe(types.string),
filters: types.frozen, // <-- This
secondaryFilters: types.frozen, // <-- This
tags: types.array(types.string),
templatesBuyedLength: types.maybe(types.number, 0)
})
... actions, views and stuff
Create the template store:
const templateStore = TemplateStore.create({
status: 'loading',
templates: [],
filters: {
isNew: undefined,
subcategoryBusiness: undefined,
subcategoryEducation: undefined,
subcategoryOthers: undefined
},
secondaryFilters: {
premium: undefined,
free: undefined,
buyed: undefined
},
tags: []
});
export { TemplateStore };
export default templateStore;
All is working fine with 1.0.1, we modified the frozen with Object.assing like an inmutable… did you change anything in version 1.1.0 about frozen that we need to understand?
Thanks!!
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 20 (20 by maintainers)
Sorry, I’m out of office this week, I will try on weekend!!
El jue., 23 nov. 2017 a las 14:18, Mattia Manzati (notifications@github.com) escribió:
@mattiamanzati damn, I spent too long redacting this and you already found the problem 😂
I think I found the problem. The behaviour of
getSnapshotfor frozen values is different between the versions:$treenodemetadata can be attached to it, this happens in node.ts#L49 wherecanAttachNodeis called and it checks withObject.isFrozentypes.frozenobjects are actually frozen in development, they don’t have$treenodeso they’re treated as a simple object every time and information about them ever being part of the tree is lostcanAttachNodereturnstrue, this adds$treenodeto object makingisStateTreeNodereturn true and causing a failure@chemitaxis the workaround for the problem that I see right now would be to avoid initializing global objects as default export and instead create an instance in place:
You could export your store object creation as a function from your file:
But I don’t think it’s a nice pattern and you should make sure all
types.optionalare set properly instead (or pass the extra data in inlined.create()).Oh, I think I know what’s going on, frozen is not froze in production, so canAttachNode() can be attached in production, but not in dev… Hard to test, but should be that! Can you please try to manually freeze the value before assigning it to check if it works?