How can I save an array of Items with AsyncStorage in react-native? So that every time you add another contact to your list it keeps adding up and not rewriting
Code:
saveContacts = ()=> {
try {
let con = {
roomId: this.state.roomId,
nickname: this.state.nickname,
}
AsyncStorage.setItem('contacts', JSON.stringify(con));
}catch(error) {
alert(error)
}
}
You can retrieve the contacts and concat the new contact to the list, then set it back into storage. Just make sure to initially set it as an empty array:
AsyncStorage.getItem('contacts')
.then((contacts) => {
const c = contacts ? JSON.parse(contacts) : [];
c.push(con);
AsyncStorage.setItem('contacts', JSON.stringify(c));
});