如何立即获得消息?它适用于新的对话(第一个块),但不适合现有对话(第二块)
以下代码适用于AddNewConvo(新对话),但不适合AddMessAgeTocConversation(现有对话)。我以为我对这两个过程都做了相同的过程,但是第二部分中我缺少一些东西。
const addNewConvo = useCallback(
(recipientId, message) => {
const newConversations = conversations.map((convo) => {
if (convo.otherUser.id === recipientId) {
// convo.messages.push(message);
// convo.latestMessageText = message.text;
// convo.id = message.conversationId;
return {
...convo,
messages: [...convo.messages, message],
latestMessageText: message.text,
id: message.conversationId
}
}
return convo
});
setConversations(newConversations);
},
[setConversations, conversations]
);
const addMessageToConversation = useCallback(
(data) => {
// if sender isn't null, that means the message needs to be put in a brand new convo
const { message, sender = null } = data;
if (sender !== null) {
const newConvo = {
id: message.conversationId,
otherUser: sender,
messages: [message],
};
newConvo.latestMessageText = message.text;
setConversations((prev) => [newConvo, ...prev]);
}
conversations.forEach((convo) => {
if (convo.id === message.conversationId) {
// convo.messages.push(message);
// convo.latestMessageText = message.text;
return {
...convo,
messages: [...convo.messages, message],
latestMessageText: message.text,
}
}
return convo
});
setConversations(conversations);
},
[setConversations, conversations]
);
The below code works for addNewConvo (new conversations), but not for addMessagetoConversation (existing conversations). I thought I did the same process for both, but I am missing something in the second part.
const addNewConvo = useCallback(
(recipientId, message) => {
const newConversations = conversations.map((convo) => {
if (convo.otherUser.id === recipientId) {
// convo.messages.push(message);
// convo.latestMessageText = message.text;
// convo.id = message.conversationId;
return {
...convo,
messages: [...convo.messages, message],
latestMessageText: message.text,
id: message.conversationId
}
}
return convo
});
setConversations(newConversations);
},
[setConversations, conversations]
);
const addMessageToConversation = useCallback(
(data) => {
// if sender isn't null, that means the message needs to be put in a brand new convo
const { message, sender = null } = data;
if (sender !== null) {
const newConvo = {
id: message.conversationId,
otherUser: sender,
messages: [message],
};
newConvo.latestMessageText = message.text;
setConversations((prev) => [newConvo, ...prev]);
}
conversations.forEach((convo) => {
if (convo.id === message.conversationId) {
// convo.messages.push(message);
// convo.latestMessageText = message.text;
return {
...convo,
messages: [...convo.messages, message],
latestMessageText: message.text,
}
}
return convo
});
setConversations(conversations);
},
[setConversations, conversations]
);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
foreach为数组中的每个元素执行回调,但不会突变数组或返回任何内容。在您的情况下,您确实想将其变异,因此您要做的就是通过对话绘制并在ID匹配的情况下更新对话。您还需要将地图的结果分配给新变量,因为它不会突变原始数组。
forEach executes a callback for every element in an array but doesn't mutate the array or return anything. In your case you do want to mutate it, so what you would do is map through the conversations and update the conversation if the id matches. You would also need to assign the results of the map to a new variable since it doesn't mutate the original array.