Importing RootStore instead of Provider #2677
-
Earlier in mobx 4-5 it was recommended to wrap your app component in a provider. I want to know if it is possible to simply import the RootStore into any component that may need it instead of using the provider/inject method. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 7 replies
-
Please read carefully through: https://mobx.js.org/react-integration.html#local-and-external-state |
Beta Was this translation helpful? Give feedback.
-
Thanks, I do not see that my issue is covered.
What I would like to do is simply export default new RootStore from my
RootStore file and then import it into my observer components.
Is there a reason to not do this? It seems to work ok.
|
Beta Was this translation helpful? Give feedback.
-
From my experience, stores-singletons could be used in case you don't have SSR and don't write unit tests. With SSR your store will be shared among all the users (because the server starts only once and your store stays the same in module between different users). For unit tests, it may be difficult to always restore the store state between test cases. Again, because the instance of a global store is shared among test cases. In other cases, it works OK. |
Beta Was this translation helpful? Give feedback.
From my experience, stores-singletons could be used in case you don't have SSR and don't write unit tests. With SSR your store will be shared among all the users (because the server starts only once and your store stays the same in module between different users). For unit tests, it may be difficult to always restore the store state between test cases. Again, because the instance of a global store is shared among test cases. In other cases, it works OK.