【发布时间】:2021-01-31 13:02:26
【问题描述】:
我在这里尝试实现的是聊天应用程序,其中最后一次对话(当前登录用户和目标用户之间)应该更新其 lastMessage 并放置在对话数组中的第一个位置。
这里是对话数组的一个更简单的版本:
const conversations = [
{ ... },
{
_id: 5,
lastMessage: {
sender: 1
receiver: 2
message: 'Hello World',
conversation_id: 5
}
},
{ ... }
]
如果id = 1 的用户向id = 2 的用户发送消息,它应该更新_id = 5 的对话并将其移动到对话数组的索引0。
我目前所做的是将对话移动到数组的顶部,但我不知道如何更改它的 lastMessage。 (反应减速器)
conversations: [state.conversations.find(el => el._id === action.payload.conversation_id), ...state.conversations.filter(el => el._id !== action.payload.conversation_id)]
感谢任何可以帮助我的人!
【问题讨论】:
标签: arrays reactjs higher-order-functions reducers