Skip to content

Dependency injection global store in child #3390

Answered by codeBelt
ko22009 asked this question in Q&A
Discussion options

You must be logged in to vote

It might be better UserStore does not initialize itself. So something like this:

class RootStore {
  constructor() {
    this.userStore = new UserStore(this);
    this.authStore = new AuthStore(this);
  }

  init() {
    this.userStore.init();
    this.authStore.init();
  }
}

class UserStore {
  rootStore;

  constructor(store) {
    this.rootStore = store;

    makeAutoObservable(this);
  }

  init() {
    this.rootStore.authStore.token;
  }
}

Replies: 2 comments 2 replies

Comment options

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

Comment options

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

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