HTB*_*HTB 5 javascript arrays react-native mobx mobx-react
我定义了一个从数组中删除项目的操作:
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) {
...
}
};
...
...
}
Run Code Online (Sandbox Code Playgroud)
尽管我将组件设置为observer
,但它仍然不会更新我的列表,直到我触发一些其他操作(单击、重命名等),在这种情况下,我将能够看到该项目已被删除。
我错过了什么吗?
尝试这个解决方案:
@action deleteItem = async (target) => {
try {
await backendService.deleteItem(target.id);
runInAction(() => {
this.items = this.items.filter(item === target);
});
} catch (error) {
...
}
};
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2953 次 |
最近记录: |