Message storage usestate
Unanswered
Checkered Giant posted this in #help-forum
Checkered GiantOP
Hey!, i got trouble while trying to store the message that my user have in the conversation
so actually i'm using a usestate with a class but idk if thats right and i got some issue
I have a class defined :
my usestate define : "const [messageInstance,setMessageInstance] = useState<stockMessages>(new stockMessages([]))"
and when i add a message i try this : setMessageInstance(new stockMessages([...newObj,...[message]]))
but actually when i receive a message my instance display only the message that i want to add and not the whole message list that i should have
so actually i'm using a usestate with a class but idk if thats right and i got some issue
I have a class defined :
class stockMessages {
messageArray : Array<ChatItemType>;
constructor(messagesArray:Array<ChatItemType>) {
this.messageArray = messagesArray
}
get getMessagesArray() {
return this.messageArray
}
get getMessagesObject() {
return this.messagesObject()
}
get getLastMessage() {
return this.lastMessage()
}
get getSortedMessageArray() {
return this.sortedArray()
}
messagesObject() {
let newMsgObj = this.messageArray.reduce(function(result:any, item:any, index:any, array:any) {
result[item.id] = item;
return result;
}, {})
return newMsgObj
}
sortedArray() {
let xTmp = []
for (let item in this.messageArray){
xTmp.push([item,this.messageArray[item].created_at,this.messageArray[item]])
}
xTmp.sort(function(a, b) {
return a[1] - b[1];
});
return xTmp.map(x=>{return x[2]})
}
lastMessage() {
let sorted = this.sortedArray()
let lastMessage = sorted[sorted.length-1]
return lastMessage
}
}my usestate define : "const [messageInstance,setMessageInstance] = useState<stockMessages>(new stockMessages([]))"
and when i add a message i try this : setMessageInstance(new stockMessages([...newObj,...[message]]))
but actually when i receive a message my instance display only the message that i want to add and not the whole message list that i should have