简体   繁体   English

对象解构返回嵌套对象

[英]Object destructuring returning nested object

I have the following initialState for my reducer:我的减速器有以下初始状态:

export function videos(
  state = { videos: [], search: { videos: [], term: "" } },
  action
)... 

In my reducer I look for persisted state and create my new videos array with some persisted props:在我的减速器中,我寻找持久化状态并使用一些持久化道具创建我的新videos数组:

case "VIDEOS_SUCCESS": {
  const { data } = action;
  let videos = [];
  let questions = [];

  data.map(item => {
    let persistedVideo =
      state.videos.find(video => video.id === item.id) || null;
    let bookMarked = persistedVideo ? persistedVideo.bookMarked : false;
    let completed = persistedVideo ? persistedVideo.completed : false;
    ....

    videos.push(newVideo);
  });

  return { ...state, videos: videos };
}

When I return my new state the videos array is nested in the video property.当我返回我的新状态时, videos数组嵌套在video属性中。

{videos:{
    search: {videos: Array(0), term: ""},
     videos: (10) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
}}

How can I get my state to look like:我怎样才能让我的状态看起来像:

{
   videos: (10) [],
   search: {videos: [], term: ''}
}

Stores always partition state according to their reducers so if you call store.getState() you should expect that the returned state is in the form商店总是根据它们的减速器来分区状态,所以如果你调用 store.getState() 你应该期望返回的状态是在形式

{
  reducer1: { /* state of reducer1 */ },
  reducer2: { /* state of reducer2 */ },
  ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM