You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is something I'm struggling with in Mobx, I have a parent store:
export class ParentStore extends StoreBase {
public subStore: SubStore;
public myObservable = [];
public myOtherObservable = [];
constructor() {
super();
this.subStore = new SubStore({
store: this,
});
makeObservable(this, {
myObservable: observable,
myOtherObservable: observable,
combined: computed,
});
autorun(() => {
console.log(this.myObservable, 'I ran, not related to the question but this one will fire');
});
}
get combined() {
return [...this.myObservable, ...this.myOtherObservable];
}
}
With an observable called myObservable. In this parent store I create a sub-store called subStore. This is the sub store:
class SubStore extends StoreBase {
store: ParentStore;
constructor({ store }: { store: ParentStore }) {
super();
this.store = store;
makeObservable(this, {
// * Computed
options: computed,
willActuallyRun: computed,
});
reaction(
() => this.store.myObservable,
(myObservable) => {
console.log(myObservable, 'This will not fire');
}
);
}
get willActuallyRun() {
// This will fire
return this.store.myObservable;
}
get options() {
// This won't fire either
return this.store.combined;
}
}
export default SubStore;
I have a reaction to myObservable in the parent store, but it will never fire. On the other hand, the computed willActuallyRun does run when myObservable in the parent store changes, if I subscribe to that sub store in a react component, wrapping it in mobx observer function, and read willActuallyRun - it will update.
I don't understand why the getter willActuallyRun does update when myObservable changes, but the reaction does not fire. Does anyone know, please?
Also, another thing (I'm sorry in advance if it's for another topic) - is it possible for the sub store to subscribe to a computed in the parent store? In this example, I would like the sub store to know when the computed called combined changes in the parent (that computed includes the two observables in the parent).
Thanks a lot in advance everyone! Please tell me if I didn't explain something well enough
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hello everyone,
This is something I'm struggling with in Mobx, I have a parent store:
With an observable called
myObservable
. In this parent store I create a sub-store calledsubStore
. This is the sub store:I have a reaction to
myObservable
in the parent store, but it will never fire. On the other hand, the computedwillActuallyRun
does run whenmyObservable
in the parent store changes, if I subscribe to that sub store in a react component, wrapping it in mobx observer function, and readwillActuallyRun
- it will update.I don't understand why the getter
willActuallyRun
does update whenmyObservable
changes, but the reaction does not fire. Does anyone know, please?Also, another thing (I'm sorry in advance if it's for another topic) - is it possible for the sub store to subscribe to a computed in the parent store? In this example, I would like the sub store to know when the computed called
combined
changes in the parent (that computed includes the two observables in the parent).Thanks a lot in advance everyone! Please tell me if I didn't explain something well enough
Beta Was this translation helpful? Give feedback.
All reactions