What is the best way to create a single MobX store for an app?

前端 未结 1 1800
孤城傲影
孤城傲影 2021-02-02 00:30

I have been using MobX and Redux for about 6 months. I have found that I prefer the simplicity of MobX for most applications. However, I like the single store concept of Redux.

相关标签:
1条回答
  • 2021-02-02 00:53

    I use MobX for a year+ now and I basically do the same:

    1) I have one "master" or "parent" store usually named as class Store {...}

    2) Then I have some smaller stores that are kept in "master" store.

    3) I prefer to construct children stores within master's store constructor. Moreover, I found that sometimes my child store has to observe some data from parent store, so I pass this into child constructors:

    class UserStore {...}
    class TodosStore {...}
    
    class Store {
      constructor() {
        this.user = new UserStore(this);
        this.todos = new TodosStore(this);
      }
    }
    

    Parent keeps references to children, and each child has reference to parent.

    0 讨论(0)
提交回复
热议问题