我在reducer中有一个redux状态如下: 帖子:
[0] post_id:1010
post_title:"Title1"
post_comments [0] id=1 Title="Some text0"
[1] id=2 Title="Some text1"
[1] post_id:1011
post_title:"Title2"
post_comments: [0] id=11 Title="Some text Comments0"
[1] id=22 Title="Some text Comments1"
当用户插入新的帖子评论时,如果我的回复为id = 33 Title =“Loreim Ispum”,我如何添加帖子评论。我需要将它添加到post_id:1011,我也作为回应。 我目前在reducer中尝试过:
items = state.posts;
items[key].comments = items[key].comments.concat(action.data);
return Object.assign({}, state, {
asyncLoading: true,
asyncError: null,
posts:items
});
提前致谢
答案 0 :(得分:0)
您可以使用传播操作符并更新
return {
...state,
asyncLoading: true,
asyncError: null,
posts: {
...state.posts.slice(0, post_index),
{
...state.posts[post_index],
post_comments: [...state.posts[post_index].post_comments, action.payload]
},
...state.posts.slice(post_index
+ 1),
}
}