Given a MyQuestionStore
store:
class MyQuestionStore {
@observable asked = 'today';
@observable answered = false;
@observable question = {
upvotes: 0,
body: null,
asker: null,
askerPoints: null,
askerBadges: null
}
// some more initial state observables...
// some actions below...
}
const myQuestionStore = new MyQuestionStore();
export default myQuestionStore;
What would be the correct way to reset all store observables back to their initial states data ('today'/false/0/null/etc..)?
NOTE: Something like MyQuestionStore.reset()
for example I think would be a good MobX way, but I don't think it exists. I would have to write an action called reset
and manually reset each observable back to its initial state. I don't think that would be the right way, since if I add more observables
, I would have to manually add them to the reset
action every time.
EDIT: this is an old answer that doesn’t work in newer versions of mobx.
If you use the reset()
to initialize the object, you could avoid duplication of code. Here is what I mean:
import { extendObservable } from "mobx";
class MyQuestionStore {
constructor() {
this.reset();
}
@action reset() {
extendObservable(this, {
asked: 'today',
answered: false,
question: {
upvotes: 0,
body: null,
asker: null,
askerPoints: null,
askerBadges: null
}
});
}
}