Using mobx-state-tree for MVVM pattern #2100
-
Hi, I am in process of defining MVVM pattern for my application and what I was thinking was have individual store for each view model. So that is if I have Login view.
and this useLoginViewModel will be used in View as follows
along with this I am planning to have a rootStore which will contain global stores which will not include view models , as view models are its individual stores I am not sure if this is the correct way of designing the store and wanted some suggestion here. Will this be any problem in future ? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
@coolsoftwaretyler Please accept my apologies for mentioning you here but I really need your suggestion on this to move forward. Can you please help me on this |
Beta Was this translation helpful? Give feedback.
-
Hi @coolsoftwaretyler Sorry to bother you again. But I have one tiny follow up on your point no 1 above "MST doesn't like interacting between sibling stores. You can do it, but you have to use unprotect, which is a yellow flag. So if you think your view-model stores will need to ever interact with the rest of your global state, consider making them a part of the global state." I am little confused regarding the siblings stores that you mentioned. Can you please help me with example. I really want to know before I dive into further regarding consequences Also if any one of View Model store needs to communicate with global store. How about wrapping the whole app with global store provider and handing of the communication to component form the ViewModel. To elaborate listening the value change in Component , which uses
Can you please help me elaborate the situation you mentioned regarding the sibling stores with an example please (For the situation where ViewModels needs to be moved to global store/ Also will it be partial move like moving only those which are needed or all of them)? Thank you very much. Eagerly waiting for your suggestions . It will really help me for finalizing a Architectural decision for the current project which is to be started. thanks |
Beta Was this translation helpful? Give feedback.
Hey @anishmiviewis - no apologies necessary. I'm happy to help.
I really like this approach. I did something similar in my app the other day and I had a really good experience with it. It's nice because you can test your MST store independently, and you can do pretty easy dependency injection for testing your React components as well. MobX-State-Tree also provides a nicer way to accomplish things that people often do by chaining
useEffect
anduseState
.Here are a couple of things I'd be wary of, but your example doesn't seem to have these issues as written:
unprotect
, which is a yellow flag. So if you…