Redux使用数组推送到第二级对象

时间:2017-09-02 06:23:57

标签: redux immutable.js

所以我有一个看起来像这样的redux商店:

posts : {
   posts: [
      0: { },
      1: { }
   ]
}

我想用这样的额外项目连接数组:

case LOAD_MORE_POSTS_SUCCESS:
  return {
    ...state,
    posts: [...state.posts, action.posts],
    isFetching: false,
  }

然而,这只是将额外的帖子推到了第一级,我尝试过做这样的事情(没有运气):

case LOAD_MORE_POSTS_SUCCESS:
  return {
    ...state,
    posts: [...state.posts.posts, action.posts],
    isFetching: false,
  }

2 个答案:

答案 0 :(得分:1)

你有没有试过像:

case LOAD_MORE_POSTS_SUCCESS:
  return {
    ...state,
    posts: {
      posts: [...state.posts.posts, action.posts]
    },
    isFetching: false,
  }

如果action.posts是一个数组(复数形式暗示它是),请考虑使用:

posts: {
  posts: [...state.posts.posts, ...action.posts]
}

答案 1 :(得分:0)

感谢@Gila Artzi - 我的最终代码看起来像这样:

case LOAD_MORE_POSTS_SUCCESS:
  return {
    ...state,
    posts: [...state.posts, ...action.posts],
    isFetching: false,
  }