【问题标题】:Perform CRUD Operation In React Native Using AsyncStorage使用 AsyncStorage 在 React Native 中执行 CRUD 操作
【发布时间】:2019-06-24 20:57:06
【问题描述】:
  *// Creating An Array
    const someArray = [1,2,3,4];
    return AsyncStorage.setItem('somekey', JSON.stringify(someArray))
    .then(json => console.log('success!'))
     .catch(error => console.log('error!'));



   //Reading An Array 
   return AsyncStorage.getItem('somekey')
  .then(req => JSON.parse(req))
  .then(json => console.log(json))
  .catch(error => console.log('error!'));
   * 

如何更新数组的特定索引并删除索引
例如,新数组应该是 {1,A,3}

【问题讨论】:

    标签: reactjs react-native async-await react-native-android react-native-ios


    【解决方案1】:

    对于 AsyncStorage,最好将数据结构视为不可变的。因此,基本上,要执行更新,您只需获取您想要的内容,然后将其弄乱,然后将其放回同一个键下。

    return AsyncStorage.getItem('somekey')
      .then(req => JSON.parse(req))
      .then(json => {
              const temp = json;
              temp[2] = 'A';
              temp.pop(); // here it's [1, A, 2]
              AsyncStorage.setItem('somekey', JSON.stringify(temp));
           })
      .catch(error => console.log('error!'));
    

    然后要删除任何项目,只需执行AsyncStorage.removeItem('somekey')AsyncStorage 没有直接操作让你做更深层次的更新,只是一个键/值数据集。

    【讨论】:

    • 我可以使用 Realm 或任何其他数据库执行 CRUD 操作吗。如果您有这样的示例,请分享。它将帮助我完成我的任务。我只需要简单的数组 CRUD 操作......
    • 有些实现人们使用 AsyncStorage,而服务基本上在云中进行管理,Realm 似乎就是其中之一。如果您想要一个完整的后端供您使用,请查看 Firebase - 加入应用程序并不难。
    • 请查看此答案以了解 react native 中的一些数据存储选项stackoverflow.com/questions/44376002/…
    猜你喜欢
    • 1970-01-01
    • 2019-11-03
    • 1970-01-01
    • 2020-12-20
    • 1970-01-01
    • 2017-09-10
    • 1970-01-01
    • 2019-01-30
    • 2023-01-05
    相关资源
    最近更新 更多