Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I inject a Store to another Store in mobX

I am using mobX in combination with React and Meteor and I need to be able to use information saved in one Store in another. Specifically, I need to have a reference of Store A in Store B in order to call an action of Store A and get the information that it had retrieved by subscribing to a collection. I used the @inject decorator but do not know how to call the action. Thank you

like image 321
Tina Raissi Avatar asked Mar 08 '23 17:03

Tina Raissi


1 Answers

@inject is used to inject something from the Provider into a React component, not between stores.

You could just import the first store into the second store and call the action straight away.

Example

// store1.js
import { observable, action } from 'mobx';

class Store1 {
  @observable count = 0;

  @action increment() {
    ++this.count;
  }
}

export default new Store1();

// store2.js
import { observable, action } from 'mobx';
import store1 from './store1';

class Store2 {
  @observable name = 'foobar';

  constructor() {
    store1.increment();
  }

  @action changeName(name) {
    this.name = name;
  }
}

export default new Store2();
like image 148
Tholle Avatar answered Mar 20 '23 19:03

Tholle