I'm working on a react app with mobx-state-tree.
I want to get some advices for 2 things.
- Q1 - Where is the better place for React.useContext?
- Q2 - Which is better to use the stored things? inherit? or useContext directly?
By the app is being expanded, components are getting more depth than before.
So, there are many components like this.
-
- One of Main Menu (ex. PostList.tsx)
-
- Sub Tab of #1 Main Menu Controller (ex. TypeXPostListController.tsx)
-
- View Component of #2 (ex. TypeXPostListView)
-
- Dialog Controller(ex. AddTypeXPostDialog)
-
- Dialog Body of #4 (ex. AddTypeXPostDialogContents)
-
- (When the dialog requires complexed things) Sub Component of #5 (ex. InputAndVerificationField)
I'm confused about the best place of the context provider and useContext for them.
Actually, #1 is under the route list and my context provider wrapped the route list at the almost near the top.
- root store provider
- route list
- #1
- route list
In this app, I have a root store and the post store is in it.
- root store
- post store
- (and other stores which are in the same level)
Here is the first question. Q1 - Where is the better place for React.useContext?
Do I need to separate the post store from the root store and set a new provider for #1?
When I searched about this, someone recommended to place it as near as can from the components which use it.
The good thing in the current structure is, there are no need to set the post store provider again for other menus (by the business reason, some other main menus require the post related features). I just call the useContext of root store and get the post store from it.
Q2 - Which place is better to use the stored things? inherit? or useContext directly?
The second question is, if I use the useContext in #5 or #6 directly again (even if I used it on the upper components already), does it impact on performance?
I'm reading some articles and React docs but the confusion is not clear yet...
If you give me any recommended search words, it also be a good help for me.
React docs provides below example. But what I wonder is (with related Q2), Button is the smallest thing in an app. Is it okay to call the context directly in the button? (as you know, theme is not only for that button and be required many other areas)
Will update my question if I get more understanding...
Thans in advance.

I'm the maintainer of MobX-State-Tree.
I recommend using this pattern to retrieve your store. It's essentially a singleton pattern, but can be overridden if you need to (such as for tests) with a provider. You do not need to use a provider for your app, though.
Then, in any component in your app, you can use it:
No need for a provider at all, as it'll just use the instantiated RootStore.
If you want to see a more complex example, the new Ignite Maverick branch has a good one:
https://github.com/infinitered/ignite/blob/bcd4a75e0aac11b40c8890c93ed2e5447d98775a/boilerplate/app/models/helpers/use-stores.ts
I'll replicate it here just in case it goes away someday: