【发布时间】:2018-09-23 20:32:29
【问题描述】:
我正在使用react-native-simple-store 并编写了以下函数来更新存储在对象数组中的所提供对象的属性。为此,我使用了issue #31 中提到的方法来删除项目。
问题是因为我使用的是 .push,更新一个项目会导致它呈现在数组的底部。这会导致项目在 FlatList 中不必要地移动。
是否有更有效的方法来更新数组中对象的属性(使用 .push 不会导致此问题)?
plusProgress = (itemId, progress) => {
const foods = this.state.foods.filter(({ id }) => itemId !== id);
const updatedItem = {
itemId,
progress: progress + 1
};
foods.push(updatedItem);
this.setState({ foods });
store.save('foods', foods);
}
【问题讨论】:
-
什么是
store? -
store是对 AsyncStorage 的导入包装器的引用(来自 react-native-simple-store)。 -
所以这样可以将数据更永久地保存在数据库或文件系统中?
标签: arrays reactjs object react-native