数组拼接不被mobx观察到
Array splice is not observed by mobx
我定义了一个从数组中删除项目的操作:
export default class myStore {
@observable items = [];
...
...
@action deleteItem = async (target) => {
try {
await backendService.deleteItem(target.id);
runInAction(() => {
const targetIndex = this.items.indexOf(target);
this.items.splice(targetIndex, 1);
});
} catch (error) {
...
}
};
...
...
}
尽管我将我的组件设为 observer
,它仍然不会更新我的列表,直到我触发一些其他操作(单击、重命名等),在这种情况下我将能够看到该项目已被删除。
我是不是漏掉了什么?
试试这个解决方案:
@action deleteItem = async (target) => {
try {
await backendService.deleteItem(target.id);
runInAction(() => {
this.items = this.items.filter(item === target);
});
} catch (error) {
...
}
};
我定义了一个从数组中删除项目的操作:
export default class myStore {
@observable items = [];
...
...
@action deleteItem = async (target) => {
try {
await backendService.deleteItem(target.id);
runInAction(() => {
const targetIndex = this.items.indexOf(target);
this.items.splice(targetIndex, 1);
});
} catch (error) {
...
}
};
...
...
}
尽管我将我的组件设为 observer
,它仍然不会更新我的列表,直到我触发一些其他操作(单击、重命名等),在这种情况下我将能够看到该项目已被删除。
我是不是漏掉了什么?
试试这个解决方案:
@action deleteItem = async (target) => {
try {
await backendService.deleteItem(target.id);
runInAction(() => {
this.items = this.items.filter(item === target);
});
} catch (error) {
...
}
};