Javascript MobX-将所有存储观察值重置回初始状态?
给定一个Javascript MobX-将所有存储观察值重置回初始状态?,javascript,mobx,Javascript,Mobx,给定一个MyQuestionStorestore: class MyQuestionStore { @observable asked = 'today'; @observable answered = false; @observable question = { upvotes: 0, body: null, asker: null, askerPoints: null, askerBadges: null } // some mo
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;
将所有存储观察值重置回其初始状态数据的正确方法是什么(“今天”/false/0/null/etc
注意:类似于
MyQuestionStore.reset()
的东西,例如,我认为这是一种很好的MobX方式,但我认为它不存在。我必须编写一个名为reset的操作,并手动将每个可观察对象重置回其初始状态。我认为这不是正确的方法,因为如果我添加更多的观察值
,我每次都必须手动将它们添加到重置
操作中。我最后不得不在重置函数中重复默认观察值,因此看起来是这样的:
class MyQuestionStore {
@observable asked = 'today';
@observable answered = false;
@observable question = {
upvotes: 0,
body: null,
asker: null,
askerPoints: null,
askerBadges: null
}
@action reset = () => {
this.asked = 'today';
this.answered = false;
this.question = {
upvotes: 0,
body: null,
asker: null,
askerPoints: null,
askerBadges: null
}
}
}
const myQuestionStore = new MyQuestionStore();
export default myQuestionStore;
const initialValue = {
name: 'John Doe',
age: 19,
}
@action reset() {
Object.keys(initialState).forEach(key => this[key] = initialState[key]);
}
我仍然觉得有一个更好的方法,而不是重复并保持干燥。我将暂时保留这个问题,希望有一个更好的干巴巴的答案。如果不需要深度重置,来自
mobx utils
的createViewModel
实用程序可能会派上用场:编辑:这是一个旧答案,在新版本的mobx中不起作用
如果使用reset()
初始化对象,可以避免代码重复。我的意思是:
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
}
});
}
}
要重置存储,可以执行以下操作:
class MyQuestionStore {
@observable asked = 'today';
@observable answered = false;
@observable question = {
upvotes: 0,
body: null,
asker: null,
askerPoints: null,
askerBadges: null
}
@action reset = () => {
this.asked = 'today';
this.answered = false;
this.question = {
upvotes: 0,
body: null,
asker: null,
askerPoints: null,
askerBadges: null
}
}
}
const myQuestionStore = new MyQuestionStore();
export default myQuestionStore;
const initialValue = {
name: 'John Doe',
age: 19,
}
@action reset() {
Object.keys(initialState).forEach(key => this[key] = initialState[key]);
}
您可以设置初始存储值:
class myStore {
constructor() {
extendObservable(this, initialState);
}
}
为什么不采用以下方法
class MyQuestionStore {
@observable asked;
@observable answered;
@observable question;
constructor() {
super()
this.init()
}
@action reset() {
this.init()
}
@action init() {
this.asked = 'today';
this.answered = false;
this.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;
MobX不跟踪以前的值,所以您所说的手动重置不是一个坏主意。我明白了。。我想即使MobX不追踪他们也没关系。我在想,也许有一种方法可以“快照”观察对象的初始状态,并在点击
reset()
时将其拉出。我只是想找出一种方法,而不必手动维护观察对象逐个重置(以防我忘了在重置中添加观察对象).你就不能基于同一类创建一个新商店吗?嗯。。。是的,我想我正在尝试深度重置,不管观察对象被更改了多少次,有点回到基线默认值。效果很好,谢谢!一个小的编辑是分号应该是逗号(因此,除非我更改6个字符,否则我不允许在答案上更改分号,我不需要这样做):extendObservable()
不会重置现有的观察值,所以听起来在构造函数之外运行reset()
实际上不会产生预期的效果。所以,简而言之,这是行不通的。@Dygerati我很有信心这段代码在旧版本的mobx中也能正常工作。我不了解mobx的最新变化,你介意添加一个适用于mobx最新版本的代码片段吗?@MouadDebbar理解,这是有道理的。到目前为止,我还不知道有什么可行的方法可以做到这一点,但如果我有什么想法,我会向大家汇报。我听说mobx的toJS
和lodashdeepMerge
的组合对OthersHMM很有效。。我不知道,因为我从未使用过lodash的deepMerge
。感谢分享和精彩的mobx!