Using an MST store inside a component #1744
Unanswered
CharlesMarlow
asked this question in
Q&A
Replies: 2 comments 2 replies
-
You have to create an instance of your model to use it. import { types } from "mobx-state-tree"
import { someStore } from './some-store';
const RootStoreModel = types.model("RootStore").props({
propertyRegistrationStore: types.optional(someStore, {}),
}).actions(self => ({
}));`
export const RootStore = RootStoreModel.create({}); Also, you should wrap your react components with |
Beta Was this translation helpful? Give feedback.
2 replies
-
I‘ve tried to make a demo according to your description ( not sure if this is what you meant ) |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi, I'm setting up MST in my existing React project but can't seem to access the store(s) I've created in the components.
My root store:
`import { types } from "mobx-state-tree"
import { someStore } from './some-store';
export const RootStore = types.model("RootStore").props({
propertyRegistrationStore: types.optional(someStore, {}),
}).actions(self => ({
}));`
My useStores setup:
`import { createContext, useContext } from "react"
import { RootStore } from "./root-store"
const RootStoreContext = createContext(RootStore);
export const RootStoreProvider = RootStoreContext.Provider
export const useStores = () => useContext(RootStoreContext)`
In my entry point file I've imported the RootStoreProvider and wrapped my entire app tree within it.
In the component, I'm importing useStores, and trying to access it both with and without destructuring, for example:
const RootStore = useStores(); const { someStore } = RootStore;
Logging the store returns undefined but debugging hasn't led anywhere so far. I guess that one of the stages isn't set up properly, perhaps I'm missing something. This is my first attempt with MST. Any help?
Beta Was this translation helpful? Give feedback.
All reactions