How is it possible, I'm creating a const to get the old messages from my chat, so I can know if has a new message comming from the database or something like that, but there is something wierd with my code, the const is changing value
const oldMessages = this.state.messages.slice();
// Console
console.log(oldMessages[oldMessages.length - 1]);
// Prints {name: "André", message: "asds", who: "Y", sending: true, local_key: 232, …}
newMessages.map((message, key) => {
// Here I don't even call the const
if (message.local_key != undefined) {
const i = messages.map((i) => {
return i.local_key
}).indexOf(message.local_key);
if (i >= 0) {
// messages.splice(i, 1);
messages[i].id = message.id;
messages[i].sending = false;
messages[i].local_key = null;
newMessages.splice(key, 1);
//
// Limpar chave do banco
//
} else {
newMessages[key].local_key = null;
}
}
if (newMessages[key] != undefined) {
newMessages[key].animate = true;
}
});
// Console
console.log(oldMessages[oldMessages.length - 1]);
// Prints {name: "André", message: "asds", who: "Y", sending: false, local_key: null, …}
You can see, I don't even call the variables, except in the consoles
Has something to do with the reference?
PS: messages
is a reference of this.state.messages
too