Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MobX - how to comunicate between stores?

In my application i have two MobX stores - store_A for handling user information (who is currently logged, etc), and store_B for handling events for all users.
After user login, i want to display all events regarding that user. How can i access logged user info (from store_A) from within store_B so that i can filter events correctly?
At this point i have to store loggeduserName data inside my store_b to retrive that data...
Code from my events store:

class ObservableEventsStore {
...
//after logIn, save userName: 
  @action setUser(userName) {
    this.givenUser = userName
  } 
...
@computed get filteredByUser() {
    let filteredByUser = this.wholeList
      .filter((event) => this.givenUser === event.user)
      // this.givenUser is what i want to get from store_A 
    return filteredByUser
  }

I want to get loggedUser data from UserStore, i have it stored there as well ...

like image 488
adamTrz Avatar asked Aug 19 '16 07:08

adamTrz


1 Answers

There is no idiomatic approach, any means to obtain a reference to the userStore is valid. I think in general you can take three approaches to achieve this:

  1. construct the userStore before the EventStore and pass the reference to the EventStore in it's constuctor (or set it afterwards)
  2. If the UserStores is a singleton, just import and use it
  3. Use a dependency injection system like InversifyJS
like image 108
mweststrate Avatar answered Oct 13 '22 10:10

mweststrate