我怎样才能在react redux-toolkit中实现这个目标?
所以,我有映射在页面中的 posts 数组。当我单击每个帖子时,它将以模式打开。
帖子有一个“喜欢”按钮,因此,我希望当用户在模式中打开帖子时喜欢该帖子,它会自动更新所有帖子都映射到的提要中的帖子喜欢状态。
现在,当我喜欢该帖子时,我会在有效负载中收到以下响应:
{
id: "620ca78f3b36d45714ca4c1e"
postId: "6dks2b92bjd99sndkd9j3",
liked: true
msg: "You liked the post."
success: true
}
现在的问题是如何从帖子数组中更新喜欢数组。
单柱结构
{
_id,
...other,
likes: [],
}
const updatedPost = (find that specific post from the array using the postId I recieved and update its likes array)
return {
...state,
loading: false,
error: null,
post: [...state.posts, updatedPost],
data: payload,
};
So, I have the posts array that is mapping in a page. when I clicked on each post it will open in modal.
Posts have a like button so, I want when user Like the post while it's open in modal it will automatically update that post liked status in that feed where all posts are mapped over.
Now, when I like the post I receive the following response in the payload:
{
id: "620ca78f3b36d45714ca4c1e"
postId: "6dks2b92bjd99sndkd9j3",
liked: true
msg: "You liked the post."
success: true
}
Now the question is how do I update the likes array from the post array.
Single Post Structure
{
_id,
...other,
likes: [],
}
const updatedPost = (find that specific post from the array using the postId I recieved and update its likes array)
return {
...state,
loading: false,
error: null,
post: [...state.posts, updatedPost],
data: payload,
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,您必须通过 id 或您正在使用的任何唯一实体查找特定帖子的索引。然后通过向其分配新的帖子详细信息来更改该索引处的帖子数组。
First you have to find index of the specific post by id or whatever unique entity you are using. And then change posts array at that index by assigning new post details to it.