Mobx-state-tree: Cannot get rootStore in nest store

Created on 17 May 2018  Â·  4Comments  Â·  Source: mobxjs/mobx-state-tree

After learn mobx quite a while, I found it really easier to use than redux but to handle the state we still need to use MST. which I found it confusing with some methods that I cannot really access and I cannot found any useful document around.

Here is the rootStore to use in the applicatoin.

const RootStore = types.model("RootStore", {
    studentStore: types.maybe(StudentStore),
    subjectStore: types.maybe(SubjectStore)
});

export default RootStore.create({
    student: StudentStore.create({
        id: "A0-2018",
        firstName: "Sync",
        lastName: "John"
    }),
    subject: SubjectStore.create()
});

SubjectStore

const Subject = types.model({
    name: types.string,
    description: types.string
});

const SubjectStore = types.model("SubjectStore", {
    subjects: types.optional(types.array(Subject), [])
}).actions(self => ({
    getSubjectByStudentId(id) {
        //Get subjects by studentId
    }
}));

I want to access to the SubjectStore inside StudentStore, and this is what I do.

const StudentStore = types.model("StudentStore", {
    id: types.identifier(),
    firstName: types.string,
    lastName: types.string,
    subjects: types.optional(types.array(Subject), [])
}).actions(self => ({
    getAllSubject() {
        let subjectStore = self.$treenode.subject;
        self.subjects = subjectStore.getSubjectByStudentId(self.id)
    }
}));

I'm not sure if this a correct behavior to access rootStore in MST, and can I use getRoot() method as mention in the document.

Most helpful comment

I think you should look a second time at the model of your problem domain,
you have no subjects living in two places: inside the
RootStore.subjectStore, and inside RootStore.studentStore.subjects. I
suspect that the latter should be a types.array(types.reference(Subject))
instead. See the concept of references in the docs for more details

Op do 24 mei 2018 om 06:26 schreef Davit Barbakadze <
[email protected]>:

It's definitely more future-proof to access root node via getRoot rather
than trying to use node properties directly.

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/mobxjs/mobx-state-tree/issues/822#issuecomment-391584114,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ABvGhMkoyWgDfHLiQK5sKoLhT4Cq82tyks5t1jZ3gaJpZM4UCfDZ
.

All 4 comments

It's definitely more future-proof to access root node via getRoot rather than trying to use node properties directly.

I think you should look a second time at the model of your problem domain,
you have no subjects living in two places: inside the
RootStore.subjectStore, and inside RootStore.studentStore.subjects. I
suspect that the latter should be a types.array(types.reference(Subject))
instead. See the concept of references in the docs for more details

Op do 24 mei 2018 om 06:26 schreef Davit Barbakadze <
[email protected]>:

It's definitely more future-proof to access root node via getRoot rather
than trying to use node properties directly.

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/mobxjs/mobx-state-tree/issues/822#issuecomment-391584114,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ABvGhMkoyWgDfHLiQK5sKoLhT4Cq82tyks5t1jZ3gaJpZM4UCfDZ
.

Nice to mention, but there is a different here. What we have here are two subject arrays, one for all students where the data already fetch from the server and other one for specific student where we want to filter it from all students store,. This where I think I can use rootStore to get advantage for the root structure. It would be nice if we can have an example to use getRoot.

Cool, I got what you mean, it will be well structure with the reference type for this store structure. Thanks

Was this page helpful?
0 / 5 - 0 ratings