将新对象添加到reducer中的对象数组

时间:2019-05-31 12:07:06

标签: reactjs typescript react-redux

我有一个在打字稿中定义如下的工作区对象:

export interface OneWorkspace {
  id: number;
  name: string;
  roles: WorkspaceRole[]
  users: WorkspaceUser[]
  logs: WorkspaceLog[]
  channels?: OneWorkspaceChannels
}

channels对象中有2个对象:groupschannels

export interface OneWorkspaceChannels {
  channels: OneWorkspaceChannel[] 
  groups: OneWorkspaceGroup[]
}

export interface OneWorkspaceChannel {
  id: number;
  name: string;
}

export interface OneWorkspaceGroup {
  id: number;
  name: string;
  channel: OneWorkspaceChannel[]
}

当用户创建一个新通道或组时,我想将新通道/组添加到我现有的工作空间对象中,并添加到channel对象中的相应对象。 在我的reducer中,对于新的频道或新的组,我得到以下值:

newChannels: OneWorkspaceChannel[] | undefined,
newGroups: OneWorkspaceGroup[] | undefined

我尝试在我的减速器中执行以下操作,但这不起作用:

case getType(actions.CreateChannelGroup.success):
  return {
    ...state,
    createChannelGroupStatus: action.payload.CreateChannelGroupStatus,
    createChannelMessage: action.payload.createChannelMessage,
    workspace: {
      ...state.workspace,
      channels: {
        ...state.workspace!.channels!,
        channels: [...state.workspace!.channels!.channels!, action.payload.newChannels! ]
      }
    }
  }

如何正确地向工作空间对象添加新频道或组?

0 个答案:

没有答案