Skip to content

Hook to access root store #1772

Answered by EmilTholin
nigellima asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @nigellima!

The first argument given to createContext will be what is returned from useContext if there is no Provider above the component in the tree. You could therefor opt for not using a provider at all and give a RootStore instance directly to createContext, as you have done in your code.

Another approach is to just export the RootStore and create an instance of if outside of the module and give it to the provider. This can help a lot if you also need to create separate instances for e.g. testing.

// stores/RootStore.ts
// ...
export const RootStore = types
  .model({
    dateFilterStore: DateFilterStore,
    gaugesStore: GaugesStore,
    continentsStore: ContinentsStore,
  });

type

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@nigellima
Comment options

Answer selected by EmilTholin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants