想要添加一些项目,显示一个列表然后能够单独删除每个项目,我对这个问题的理解是我必须创建一个数组,然后每次我想删除一个项目然后保存就更新它。这对于基本的数据库操作来说似乎相当复杂,我尝试了几种方法,不知道我的代码是否是我应该做的。有人可以指出正确的方向吗?
我面临的最后一个错误:
对象为null或未定义 C:\ Users \用户OLED \库存\ node_modules \反应天然\库\ polyfills \ Array.es6.js:24:26 _toConsumableArray
FetchValue = () => {
AsyncStorage.getItem("Favorites").then((value) => {
this.setState({
favs: JSON.parse(value)
});
}).done();
};
SaveValue = () => {
const newFavs = [...this.state.favs, this.state.UserInput];
this.setState({ favs: newFavs, UserInput: '' }, () => {
AsyncStorage.setItem("Favorites", JSON.stringify(this.state.favs));
Keyboard.dismiss()
});
};
RemoveValue(item){
const index = this.state.favs.indexOf(item);
const newArray = [...this.state.favs];
newArray.splice(index,1);
this.setState({ favs: newArray });
AsyncStorage.setItem("Favorites", JSON.stringify(newArray));
}
答案 0 :(得分:0)
我认为删除最后一个元素后,你的数组是空的。因此,每当您尝试从数组中删除值时,它将给您错误。因此,请检查数组计数是否大于0的情况。
我希望它对你有用。
答案 1 :(得分:0)
使用此代码替换 RemoveValue 方法。
我们需要检查 项是否 null / undefined
{{1}}
这将解决您的崩溃问题。