En React Native, quiero almacenar datos de matriz de objetos con una identificación única cada vez que escribo en el cuadro de texto. Estoy usando almacenamiento asíncrono pero no sé cómo manejar eso.
Podrías implementar algo como esto
Primer intento
npm i @react-native-async-storage/async-storageY luego cree un AsyncStore.js o un archivo similar:
import AsyncStorage from '@react-native-async-storage/async-storage'; export const storeObject = async (object) => { const id = await createId() const identified = {...object, id } const jsonValue = JSON.stringify(identified); await AsyncStorage.setItem('@object-' + id, jsonValue); }; export const getObject = async (id) => { const storedObject = await AsyncStorage.getItem('@object-' + id); if (storedObject){ return JSON.parse(storedObject ); } }; export const storeIds = async (ids) => { const jsonValue = JSON.stringify(ids); await AsyncStorage.setItem('@object-ids', jsonValue); }; export const getIds = async (id, ids) => { const storedIds = await AsyncStorage.getItem('@object-ids'); if (storedIds) { return JSON.parse(storedIds ); } }; const createId = async() => { const ids = await getIds(); const id = Math.max(ids) + 1 await storeIds([...ids,id]) return id; }Editar: agregué un código de identificador único no probado