简体   繁体   中英

How can I store unique data with asyncstorage in React Native?

In React Native I want to store object array data with a unique id each time I type in the textbox. I'm using async storage but don't know how to handle that.

You could implement something like this

First run

npm i @react-native-async-storage/async-storage

And then create a AsyncStore.js or similar file:

import AsyncStorage from '@react-native-async-storage/async-storage';

export const storeObject = async (id, object) => {
  const jsonValue = JSON.stringify(object);
  await AsyncStorage.setItem('@object-' + id, jsonValue);
};

export const getObject = async (id) => {
  const storedObject = await AsyncStorage.getItem('@object-' + id);
  if (storedObject) {
    return JSON.parse(storedObject );
     
  }
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM