arrays - React Native AsyncStorage 不覆盖数据
问题描述
我正在尝试制作一个应用程序,并且我在使用 AsyncStorage 的数组中有一些对象。我需要更改对象的一个元素,但如果我尝试更改它,它不会保留这些更改。
我的代码:
save = async () => {
this.state.dataSource.notes = this.state.text;
try {
AsyncStorage.clear();
await AsyncStorage.setItem('medicines', JSON.stringify(this.state.dataSource));
} catch (error) {
console.log('error');
}
this.setModalVisible(!this.state.modalVisible);
存储文本输入的this.state.text
值。
this.state.dataSource
存储对象数组。
这两个都像他们应该的那样工作。
数组如下所示:
Array = [
item = {
id: 'id',
name: 'name',
brand: 'brand',
inname: 'inname',
chosenWeekDay: 'week day',
androidDate: 'date for android',
chosenAndroidTime: 'time for android',
notes: 'notes in a string',
}]
解决方案
您需要针对dataSource
数组中的特定对象
let { dataSource, text } = this.state;
const yourSpecfiicIndex = 12; // whatever
dataSource[yourSpecfiicIndex].notes = text;
//...
await AsyncStorage.setItem('medicines', JSON.stringify(dataSource));
推荐阅读
- vue.js - 如何渲染一个包含 3 个项目和 2 列的循环,其中 1 个在第一列,另外 2 个在第 2 列?
- c++11 - 自己声明结构?
- oracle - 将 Informix 查询转换为 Oracle?
- vba - 循环遍历“条形样式”中的项目,阅读项目
- android - Android Google Play 商店应用程序不兼容 64 位
- unity3d - 如何更改附加了动画师的 UI 图像?
- ibm-cloud - Watson Assistant:我可以使用示例中的实体来定义意图吗?
- python - 使用 python pandas 将现有的 excel 表附加到新的数据框,而不加载旧的
- java - 在不和谐机器人上上传用户文件
- azure - HeadNode,workers Azure 中的节点大小详细信息