Anytime the list updates, it forms another array that adds to the previous array, how can I make it to return just one array with the updated information
below is the code
useEffect(() => {
firebase.database().ref('/users/' + '/messages/').orderByChild('createdAt').on('value', function(snapshot) {
snapshot.forEach(function(userSnapshot) {
const id = userSnapshot.key;
const userData = userSnapshot.val();
setId(id);
messages.push(userData);
});
});
setMessages(messages);
}, [messages])
usage in body
<ul>
{messages.map(function(item){
return (<li key={id}>{item.displayName}: {item.text}</li>)
})}
</ul>
when the list updates once, it returns two more lists, so the new list becomes very very long