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
@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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With