MST to mobx-keystone: bonus points, pain points, barriers to groking #303
Replies: 16 comments 8 replies
-
mobx-keystone has two APIs for defining model props:
|
Beta Was this translation helpful? Give feedback.
-
A common pattern I used in MST was to create mobx reactions on |
Beta Was this translation helpful? Give feedback.
-
Major +1 that I could just replace |
Beta Was this translation helpful? Give feedback.
-
🙌 yay to runtime type checking only where you need it. Most of my models don't need it and the typescript type generics work nicely. |
Beta Was this translation helpful? Give feedback.
-
+1 for Path as array of strings, the syntax of JSONPath had me mixed up for a while and with this feature, no more string wrangling e.g. joinJSONPath |
Beta Was this translation helpful? Give feedback.
-
One little gotcha: you can't destructure model methods like you could in MST or else |
Beta Was this translation helpful? Give feedback.
-
mobx-keystone models require This makes it difficult to work with MST data. IMO it would be great if we didn't need |
Beta Was this translation helpful? Give feedback.
-
I got tripped up about the data property of the model constructors. In MST, the create property can be a snapshot but here we must use fromSnapshot I now see. I observed that fromSnapshot was not able to resolve the type for a union type property. At first I thought it was because I wasn't using
and instead had to do
This forced me to add some extra conditional logic to my code. In this case it is fine, but in other places I need to restore a snapshot of a deeply nested tree containing the texture and thus I am under the impression that after many days of working to convert my project from MST, I may have to abandon this translation or use some kind of hacky workaround where I create 2 "maybe" props 😞 I hope I'm wrong, is this a bug or just confusion? |
Beta Was this translation helpful? Give feedback.
-
In MST, I was able to return instantiated models from views' getters but this doesn't seem to work unless I first store the instance as a property of the class and at that point it's useless to define a getter and I might as well use a reaction to set a class attribute instead of having a getter and an attribute. This creates a lack of consistency around syntax for computed props in my app and adds an extra step of dealing with the disposer. |
Beta Was this translation helpful? Give feedback.
-
@AndrewMorsillo the solution with
does not work because the model also needs a $modelId for both the TextureModel and the PathFaceDecorationPatternModel 😖 |
Beta Was this translation helpful? Give feedback.
-
EDIT: this is irrelevant. I mistakenly thought prop types coudn't have undefined but this is wrong, it's only arrays that can't store undefined Minor gotcha: when converting from MST, I had a maybe type that I converted to an | null type, this breaks de-structuring of what was possibly undefined value but now is possibly null. |
Beta Was this translation helpful? Give feedback.
-
Bonus: with MST, I was storing reaction disposers in volatile state array to be executed on beforeDestroy. With keystone, disposers can remain scoped to onAttachedToRootStore and the returned teardown function follows a familiar pattern like in React's useEffect |
Beta Was this translation helpful? Give feedback.
-
A major pain point for me is the requirement that a model be attached to a root store in order to have lifecycle methods via onAttachedToRootStore. This forces me to move transient models into its parent model properties instead of making them computed properties or @observable attributes (where I can leverage |
Beta Was this translation helpful? Give feedback.
-
Gotcha: default references. Use case: I have some "static" models representing SVG dasharray variants, and a model that references these different styles of dotted lines. I create an array of (unattached) dasharray models and exported them, and in this way I could create a default reference as the first model in the array of dasharray models like |
Beta Was this translation helpful? Give feedback.
-
The use of context is a real game-changer for me 🙌 Coming from MST, I was looking for a way to get volatile data initialized at creation time. Supplying a context at model creation time is THE reason why I decided to switch over from MST. It has made my models much cleaner in the case of having a DOM node dependency for my model when orchestrating a Three.js scene in a model. |
Beta Was this translation helpful? Give feedback.
-
Here's a quick reminder, that might save someone an hour of wasted time. Coming from MST, you could have a view that invoked an action directly on an MST store. <Button onClick={myStore.doAction} /> Which has been the pattern in RN for a while now (i.e. not using arrow methods within views for various reasons). However, I didn't think about this when creating our first keystone model. Because the So keep that in mind when referencing action methods in your keystone models classes, from directly within your view. |
Beta Was this translation helpful? Give feedback.
-
Hi folks, I'm in the process of converting an MST project to mobx-keystone. I wanted to spark up some discussion around the process in hopes of crowd-sourcing knowledge on how to make the leap. Perhaps it could help raise awareness about the risks and benefits of adoption.
One of the things I like about this library is that I don't have to use MST Instance for type comparison. It seems instead I can just do
myInstance instanceof MyClass
. It was a minor leap to go fromgetParentOfType(this, MyModel)
tofindParent<MyModel>(this, (parentNode) => parentNode instanceof MyModel)
.One of the conceptual barriers I'm experiencing is the relationship between the
types
export and props. For example:prop<AppleModel | OrangeModel>()
🤞types
has has properties for primitives e.g.types.number
but the docs demonstratesprop<number>
so I'm not sure what that's fortranslate: prop<RawPoint>(() => ({ x: 0, y: 0 })),
into an immutable instance. The data property intermediary accessor makes it dissimilar to MST so I wonder if a rename to types.immutable would help to disambiguate.I'd appreciate hearing more about others' experiences.
Beta Was this translation helpful? Give feedback.
All reactions